Observable.FromAsyncPattern<T1、TResult> メソッド (Func<T1、AsyncCallback、Object、IAsyncResult>、Func<IAsyncResult、TResult>)

Begin/End 呼び出し関数ペアを非同期関数に変換します。

Namespace:System.Reactive.Linq
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'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.

型パラメーター

  • T1
    関数の最初の型。
  • TResult
    結果の型。

パラメーター

戻り値

種類: System.Func<T1、 IObservable<TResult>>
Begin/End 呼び出し関数のペア。

解説

FromAsyncPattern 演算子は、非同期呼び出しの作成を簡略化するために使用されます。 非同期呼び出しをラップして、非同期呼び出しを処理する非同期関数を使用して開始/終了呼び出しを行います。 関数は、結果と同じ型の監視可能なシーケンスを返します。 たとえば、System.IO.Directory.GetFiles への非同期呼び出しを設定できます。 そのメソッドの結果は、要求されたファイルの一覧を含む文字列配列です。 そのため、FromAsyncPattern 演算子から返される非同期関数は、観察可能な文字列 [] のシーケンスを返します。 これは、このトピックのコード例で示されています。 この演算子には、異なる数の入力パラメーターを受け取るメソッド呼び出しを処理するためのさまざまなオーバーロードがあります。 非同期呼び出しを設定するには、FromAsyncPattern 演算子の呼び出しで型を指定します。 指定された最後の型は戻り値の型です。 たとえば、System.IO.Directory.GetFiles は最大 3 つの入力パラメーターを受け取り、結果として文字列配列を返すことができます。 次のコード スニペットは、型の順序を示しています。

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);

この例では、System.IO.Direcotry.GetFile を非同期的に呼び出して、C:\Program Files ディレクトリのすべてのファイルを列挙する方法を示します。 この例では、FromAsyncPattern 演算子によって提供される非同期関数を使用します。 アクション イベント ハンドラーは、結果の各ファイル名をコンソール ウィンドウに書き込む非同期呼び出しのコールバックとして機能します。

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();
    }
  }
}

次の出力例は、コード例によって生成されます。

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

参照

リファレンス

Observable クラス

FromAsyncPattern オーバーロード

System.Reactive.Linq 名前空間