Observable.FromAsyncPattern < T1, TResult > 方法 (Func < T1, AsyncCallback, Object, IAsyncResult > , Func < IAsyncResult, TResult >)
將 Begin/End invoke 函式組轉換成非同步函式。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
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.
類型參數
- T1
函式的第一種類型。
- TResult
結果的類型。
參數
- begin
類型:System.Func< T1、AsyncCallback、Object、IAsyncResult>
begin invoke 函式。
- end
類型:System.Func<IAsyncResult、TResult>
結束叫用函式。
傳回值
類型:System.Func< T1、IObservable< TResult>>
Begin/End invoke 函式配對。
備註
FromAsyncPattern 運算子可用來簡化進行非同步呼叫。 它會使用非同步函式包裝非同步呼叫,以開始/結束叫用,以為您處理非同步呼叫。 函式會傳回與結果相同類型的可觀察序列。 例如,您可以設定 System.IO.Directory.GetFiles 的非同步呼叫。 該方法的結果是包含所要求檔案清單的字串陣列。 因此,從 FromAsyncPattern 運算子傳回的非同步函式會傳回可觀察的字串序列[]。 本主題的範例程式碼會示範這一點。 此運算子有各種多載,可處理採用不同數目輸入參數的方法呼叫。 若要設定非同步呼叫,請使用 FromAsyncPattern 運算子的呼叫來指定型別。 指定的最終型別是傳回實值型別。 例如,System.IO.Directory.GetFiles 最多可能需要三個輸入參數,並傳回字串陣列作為結果。 下列程式碼片段顯示類型的順序。
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