Partilhar via


Método TSource Observable.ToObservable<(IEnumerable<TSource>, IScheduler)>

Converte uma sequência enumerável em uma sequência observável com uma origem e um agendador especificados.

Namespace:System.Reactive.Linq
Assembly: System.Reactive (em System.Reactive.dll)

Sintaxe

'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.

Parâmetros de tipo

  • TSource
    O tipo de fonte.

Parâmetros

Valor Retornado

Tipo: System.IObservable<TSource>
A sequência observável cujos elementos são extraídos da sequência enumerável fornecida.

Observação de uso

No Visual Basic e no C#, você pode chamar esse método como um método de instância em qualquer objeto do tipo IEnumerable<TSource>. Quando você usar a sintaxe de método de instância para chamar esse método, omita o primeiro parâmetro. Para obter mais informações, consulte ou .

Comentários

O operador ToObservable cria uma sequência observável de um objeto que dá suporte à interface IEnumerable. Um parâmetro de agendador é fornecido com esse operador para permitir diferentes opções de agendamento para o processamento necessário para criar a sequência observável. Por exemplo, talvez você queira agendar o processamento de enumeração necessário para ocorrer em outro thread.

Exemplos

Este exemplo cria uma sequência observável de cadeias de caracteres (cadeia de caracteres IObservable<>) da cadeia de caracteres IEnumerable<> exposta pela matriz de cadeia de caracteres retornada do System.IO. Método Directory.GetDirectories. O agendador ThreadPoolScheduler é passado para o parâmetro agendador para o operador ToObservable. Isso fará com que a enumeração seja executada em um thread do pool de threads do .NET. Portanto, o thread main não será bloqueado.

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();
    }
  }
}

A saída a seguir foi gerada usando o código de exemplo.

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

Consulte Também

Referência

Classe observável

Sobrecarga ToObservable

System.Reactive.Linq Namespace