Observable.ToObservable < TSource 方法 (IEnumerable < TSource >> 、IScheduler)
使用指定的來源和排程器,將可列舉序列轉換為可觀察的序列。
Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (
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# 中,您可以在IEnumerable< TSource > 類型的任何物件上呼叫此方法作為實例方法。 使用執行個體方法語法呼叫這個方法時,請省略第一個參數。 如需詳細資訊,請參閱 或 。
備註
ToObservable 運算子會從支援 IEnumerable 介面的物件建立可觀察的序列。 此運算子會提供排程器參數,以允許建立可觀察序列所需的處理不同排程選項。 例如,您可能想要排程在另一個執行緒上發生的列舉處理。
範例
這個範例會從 System.IO 傳回之字串陣列所公開的 IEnumerable) 字串,建立 (IObservable << 字串 > 的可觀察字串 > 序列。 Directory.GetDirectories 方法。 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