지정된 원본 및 스케줄러를 사용하여 열거 가능한 시퀀스를 관찰 가능한 시퀀스로 변환합니다.
네임스페이스:System.Reactive.Linq
어셈블리: System.Reactive(System.Reactive.dll)
Syntax
'Declaration
<ExtensionAttribute> _
Public Shared Function ToObservable(Of TSource) ( _
source As IEnumerable(Of TSource), _
scheduler As IScheduler _
) As IObservable(Of TSource)
'Usage
Dim source As IEnumerable(Of TSource)
Dim scheduler As IScheduler
Dim returnValue As IObservable(Of TSource)
returnValue = source.ToObservable(scheduler)
public static IObservable<TSource> ToObservable<TSource>(
this IEnumerable<TSource> source,
IScheduler scheduler
)
[ExtensionAttribute]
public:
generic<typename TSource>
static IObservable<TSource>^ ToObservable(
IEnumerable<TSource>^ source,
IScheduler^ scheduler
)
static member ToObservable :
source:IEnumerable<'TSource> *
scheduler:IScheduler -> IObservable<'TSource>
JScript does not support generic types and methods.
형식 매개 변수
- TSource
원본의 형식입니다.
매개 변수
- source
형식: System.Collections.Generic.IEnumerable<TSource>
관찰 가능한 시퀀스로 변환할 열거 가능한 시퀀스입니다.
- scheduler
형식: System.Reactive.Concurrency.IScheduler
입력 시퀀스의 열거형을 실행할 스케줄러입니다.
반환 값
형식: System.IObservable<TSource>
지정된 열거 가능한 시퀀스에서 요소를 끌어오는 관찰 가능한 시퀀스입니다.
사용 정보
Visual Basic 및 C#에서는 IEnumerableTSource> 형식의 모든 개체에서 이 메서드를 instance 메서드로 호출할 수 있습니다<. 인스턴스 메서드 구문을 사용하여 이 메서드를 호출할 경우에는 첫 번째 매개 변수를 생략합니다. 자세한 내용은 또는 를 참조하세요.
설명
ToObservable 연산자는 IEnumerable 인터페이스를 지원하는 개체에서 관찰 가능한 시퀀스를 만듭니다. 스케줄러 매개 변수는 관찰 가능한 시퀀스를 만드는 데 필요한 처리에 대한 다양한 일정 옵션을 허용하기 위해 이 연산자와 함께 제공됩니다. 예를 들어 다른 스레드에서 발생하는 데 필요한 열거형 처리를 예약할 수 있습니다.
예제
이 예제에서는 System.IO 반환된 문자열 배열에 의해 노출되는 IEnumerable 문자열에서 관찰 가능한 문자열 시퀀스(IObservable<<> 문자열>)를 만듭니다. Directory.GetDirectories 메서드. Scheduler 매개 변수에 대해 ThreadPoolScheduler 스케줄러 가 ToObservable 연산자에 전달됩니다. 이렇게 하면 .NET 스레드 풀의 스레드에서 열거형이 실행됩니다. 따라서 기본 스레드가 차단되지 않습니다.
using System;
using System.IO;
using System.Reactive.Linq;
using System.Reactive.Concurrency;
namespace Example
{
class Program
{
static void Main()
{
//*********************************************************************************************************************//
//*** Create a new observable sequence from the IEnumerable<String> exposed by the string array returned from ***//
//*** System.IO.Directory.GetDirectories(). ***//
//*** ***//
//*** In this example we use the ThreadPool scheduler to run the enumeration on a thread in the .NET thread pool. ***//
//*** This way our main thread is not blocked by the enumeration and we can process user interaction. ***//
//*********************************************************************************************************************//
var fileList = Directory.GetDirectories(@"C:\Program Files");
var seqfiles = fileList.ToObservable(Scheduler.ThreadPool);
//*********************************************************************************************************************//
//*** We subscribe to this sequence with a lambda expression as the action event handler for the OnNext action. It ***//
//*** writes each filename to the console window. A action event handler for the OnCompleted action is also ***//
//*** provided to inform the user the sequence has ended and prompt for the ENTER key. ***//
//*********************************************************************************************************************//
Console.WriteLine("\nCreating subscription. Press ENTER to exit...\n");
seqfiles.Subscribe(f => Console.WriteLine(f.ToString()));
//*********************************************************************************************************************//
//*** Since we used the ThreadPool scheduler when creating the observable sequence, the enumeration is running on a ***//
//*** thread from the .NET thread pool. So the main thread is not blocked and can terminate the example if the user ***//
//*** presses ENTER for a long running enumeration. ***//
//*********************************************************************************************************************//
Console.ReadLine();
}
}
}
다음 출력은 예제 코드를 사용하여 생성되었습니다.
Creating subscription. Press ENTER to exit...
C:\Program Files\Common Files
C:\Program Files\IIS
C:\Program Files\Internet Explorer
C:\Program Files\Microsoft Games
C:\Program Files\Microsoft Help Viewer
C:\Program Files\Microsoft IntelliType Pro
C:\Program Files\Microsoft LifeCam
C:\Program Files\Microsoft Lync
C:\Program Files\Microsoft Office
C:\Program Files\Microsoft SDKs
C:\Program Files\Microsoft Security Client
C:\Program Files\Microsoft SQL Server
C:\Program Files\Microsoft SQL Server Compact Edition
C:\Program Files\Microsoft Sync Framework
C:\Program Files\Microsoft Synchronization Services
C:\Program Files\Microsoft Visual Studio 10.0
C:\Program Files\Microsoft Visual Studio 9.0
C:\Program Files\Microsoft.NET
C:\Program Files\MSBuild
C:\Program Files\Reference Assemblies
C:\Program Files\Uninstall Information
C:\Program Files\Windows Journal
C:\Program Files\Windows Live
C:\Program Files\Windows Mail
C:\Program Files\Windows Media Components
C:\Program Files\Windows Media Player
C:\Program Files\Windows NT
C:\Program Files\Windows Photo Viewer
C:\Program Files\Windows Portable Devices
C:\Program Files\Windows Sidebar
C:\Program Files\Zune