Observable.FromAsyncPattern<T1, TResult> Method (Func<T1, AsyncCallback, Object, IAsyncResult>, Func<IAsyncResult, TResult>)

Converts a Begin/End invoke function pair into an asynchronous function.

Namespace:  System.Reactive.Linq
Assembly:  System.Reactive (in System.Reactive.dll)

Syntax

'Declaration
Public Shared Function FromAsyncPattern(Of T1, TResult) ( _
    begin As Func(Of T1, AsyncCallback, Object, IAsyncResult), _
    end As Func(Of IAsyncResult, TResult) _
) As Func(Of T1, IObservable(Of TResult))
'Usage
Dim begin As Func(Of T1, AsyncCallback, Object, IAsyncResult)
Dim end As Func(Of IAsyncResult, TResult)
Dim returnValue As Func(Of T1, IObservable(Of TResult))

returnValue = Observable.FromAsyncPattern(begin, _
    end)
public static Func<T1, IObservable<TResult>> FromAsyncPattern<T1, TResult>(
    Func<T1, AsyncCallback, Object, IAsyncResult> begin,
    Func<IAsyncResult, TResult> end
)
public:
generic<typename T1, typename TResult>
static Func<T1, IObservable<TResult>^>^ FromAsyncPattern(
    Func<T1, AsyncCallback^, Object^, IAsyncResult^>^ begin, 
    Func<IAsyncResult^, TResult>^ end
)
static member FromAsyncPattern : 
        begin:Func<'T1, AsyncCallback, Object, IAsyncResult> * 
        end:Func<IAsyncResult, 'TResult> -> Func<'T1, IObservable<'TResult>> 
JScript does not support generic types and methods.

Type Parameters

  • T1
    The first type of function.
  • TResult
    The type of result.

Parameters

Return Value

Type: System.Func<T1, IObservable<TResult>>
A Begin/End invoke function pair.

Remarks

The FromAsyncPattern operator is used to simplify making asynchronous calls. It wraps an asynchronous call to begin/end invoke with an asynchronous function which handles the asynchronous call for you. The function returns an observable sequence which is of the same type as the result. For example, you could setup an asynchronous call to System.IO.Directory.GetFiles. The result of that method is a string array which contains the list of files requested. So the asynchronous function returned from the FromAsyncPattern operator would return an observable sequence of string[]. This is demonstrated in the example code for this topic. There are various overloads of this operator to handle method calls that take a different number of input parameters. To setup a asynchronous call specify the types with the call to the FromAsyncPattern operator. The final type specified is the return value type. For example, System.IO.Directory.GetFiles can take up to three input parameters and returns a string array as the result. The following code snippet shows the order of the types.

delegate string[] GetFilesDelegate(string searchPath, string searchPattern, SearchOption searchOption);


GetFilesDelegate getFiles = Directory.GetFiles;


//**************************************************************************************************//
//***  Observable.FromAsyncPattern<Param 1 Type, Param 2 Type, Param 3 Type, Return value type>  ***//
//**************************************************************************************************//
var getFileList = Observable.FromAsyncPattern<string, string, SearchOption, string[]>(getFiles.BeginInvoke, getFiles.EndInvoke);

Examples

This example demonstrates calling System.IO.Direcotry.GetFile asynchronously to enumerate all files under the C:\Program Files directory. The example uses the asynchronous function provided by the FromAsyncPattern operator. An action event handler acts as the callback for the asynchronous call to write each filename in the result to the console window.

using System;
using System.Reactive.Linq;
using System.IO;

namespace Example
{                                       
  delegate string[] GetFilesDelegate(string searchPath, string searchPattern, SearchOption searchOption);

  class Program
  {
    static void Main()
    {                                                                                                                    
      //********************************************************************************************************************//
      //*** For this example, Reactive Extensions is used to wrap an asynchronous call that recursively enumerates files ***//
      //*** in a given directory.                                                                                        ***//
      //********************************************************************************************************************//
      string mySearchPath = "C:\\Program Files";                                                                                   
      GetFilesDelegate getFiles = Directory.GetFiles;


      //*****************************************************************************************************************************//
      //*** Reactive Extensions will wrap the asynchronous call to the delegate returning the asynchronous function, getFileList. ***//
      //*** Calling the asynchronous function returns the observable sequence of the string[].                                    ***//
      //***                                                                                                                       ***//
      //*** There are many overloaded versions of the FromAsyncPattern operator. The types signified here are based on parameters ***//
      //*** in the signature of actual method being called asynchronously. The types are specified in their proper order followed ***//
      //*** by the return type (ex. <Param 1 type, Param 2 type, Param 3 type, return type> ).                                    ***//
      //*****************************************************************************************************************************//
      var getFileList = Observable.FromAsyncPattern<string, string, SearchOption, string[]>(getFiles.BeginInvoke, getFiles.EndInvoke);    
      IObservable<string[]> fileObservable = getFileList(mySearchPath,"*.*",SearchOption.AllDirectories);


      //*********************************************************************************************************************//
      //*** We subscribe to this sequence with an action event handler defined with the lambda expression. It acts as the ***//
      //*** callback for completion of the asynchronous operation.                                                        ***//
      //*********************************************************************************************************************//
      fileObservable.Subscribe(fileList =>
      {
        foreach (string f in fileList)
        {
          Console.WriteLine(f.ToString());
        }
      });


      Console.WriteLine("Running async enumeration of the {0} directory.\n\nPress ENTER to cancel...\n",mySearchPath);
      Console.ReadLine();
    }
  }
}

The following example output is generated by the example code.

Running async enumeration of the C:\Program Files directory.

Press ENTER to cancel...

C:\Program Files\desktop.ini
C:\Program Files\ATI\CIM\Bin64\atdcm64a.sys
C:\Program Files\ATI\CIM\Bin64\ATILog.dll
C:\Program Files\ATI\CIM\Bin64\ATIManifestDLMExt.dll
C:\Program Files\ATI\CIM\Bin64\ATISetup.exe
C:\Program Files\ATI\CIM\Bin64\CompressionDLMExt.dll
C:\Program Files\ATI\CIM\Bin64\CRCVerDLMExt.dll
C:\Program Files\ATI\CIM\Bin64\DetectionManager.dll
C:\Program Files\ATI\CIM\Bin64\difxapi.dll
C:\Program Files\ATI\CIM\Bin64\DLMCom.dll
C:\Program Files\ATI\CIM\Bin64\EncryptionDLMExt.dll
C:\Program Files\ATI\CIM\Bin64\InstallManager.dll
C:\Program Files\ATI\CIM\Bin64\InstallManagerApp.exe
C:\Program Files\ATI\CIM\Bin64\InstallManagerApp.exe.manifest
C:\Program Files\ATI\CIM\Bin64\LanguageMgr.dll
C:\Program Files\ATI\CIM\Bin64\mfc80u.dll
C:\Program Files\ATI\CIM\Bin64\Microsoft.VC80.ATL.manifest
C:\Program Files\ATI\CIM\Bin64\Microsoft.VC80.CRT.manifest
C:\Program Files\ATI\CIM\Bin64\Microsoft.VC80.MFC.manifest
C:\Program Files\ATI\CIM\Bin64\Microsoft.VC80.MFCLOC.manifest
C:\Program Files\ATI\CIM\Bin64\Microsoft.VC80.OpenMP.manifest
C:\Program Files\ATI\CIM\Bin64\msvcp80.dll
C:\Program Files\ATI\CIM\Bin64\msvcr80.dll
C:\Program Files\ATI\CIM\Bin64\PackageManager.dll
C:\Program Files\ATI\CIM\Bin64\readme.rtf
C:\Program Files\ATI\CIM\Bin64\SetACL64.exe

See Also

Reference

Observable Class

FromAsyncPattern Overload

System.Reactive.Linq Namespace