Begin/End 호출 함수 쌍을 비동기 함수로 변환합니다.
네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(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.
형식 매개 변수
- T1
함수의 첫 번째 형식입니다.
- TResult
결과의 형식입니다.
매개 변수
- begin
형식: System.Func<T1, AsyncCallback, Object, IAsyncResult>
begin invoke 함수입니다.
- end
형식: System.Func<IAsyncResult, TResult>
end invoke 함수입니다.
반환 값
형식: 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