将 Begin/End 调用函数对转换为异步函数。
Namespace:System.Reactive.Linq
装配: System.Reactive.dll) 中的 System.Reactive (
语法
'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 调用函数。
- end
类型: System.Func<IAsyncResult、TResult>
end invoke 函数。
返回值
类型: System.Func<T1、 IObservable<TResult>>
开始/结束调用函数对。
备注
FromAsyncPattern 运算符用于简化异步调用。 它用一个处理异步调用的异步函数包装对开始/结束调用的异步调用。 函数返回与结果类型相同的可观察序列。 例如,可以设置对 System.IO.Directory.GetFiles 的异步调用。 该方法的结果是包含所请求文件列表的字符串数组。 因此,从 FromAsyncPattern 运算符返回的异步函数将返回 string[] 的可观测序列。 本主题的示例代码对此进行了演示。 此运算符有多种重载,用于处理采用不同数量输入参数的方法调用。 若要设置异步调用,请指定调用 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