共用方式為


Observable.Create < TSource > 方法 (Func < IObserver < TSource > 、IDisposable >)

從訂閱者法實作建立可觀察的序列。

Namespace:System.Reactive.Linq
裝配: System.Reactive.dll) 中的 System.Reactive (

Syntax

'Declaration
Public Shared Function Create(Of TSource) ( _
    subscribe As Func(Of IObserver(Of TSource), IDisposable) _
) As IObservable(Of TSource)
'Usage
Dim subscribe As Func(Of IObserver(Of TSource), IDisposable)
Dim returnValue As IObservable(Of TSource)

returnValue = Observable.Create(subscribe)
public static IObservable<TSource> Create<TSource>(
    Func<IObserver<TSource>, IDisposable> subscribe
)
public:
generic<typename TSource>
static IObservable<TSource>^ Create(
    Func<IObserver<TSource>^, IDisposable^>^ subscribe
)
static member Create : 
        subscribe:Func<IObserver<'TSource>, IDisposable> -> IObservable<'TSource> 
JScript does not support generic types and methods.

類型參數

  • TSource
    來源的類型。

參數

傳回值

類型:System.IObservable< TSource>
具有訂閱者法之指定實作的可觀察序列。

備註

Create 運算子可讓您建立自己的自訂序列,而不需要完整實作序列的 IObservable < T > 介面。 使用這個運算子,您只需要實作一個訂閱函式,它會採用 T 是您的型別的 IObserver < T > ,並傳回用來藉由呼叫 IDisposable::D ispose () 來取消訂閱的 IDisposable 介面。 此運算子的用法優先于 IObservable < T > 介面的手動實作。

範例

此範例會模擬假設票證系統,其中提供可觀察的票證序列,而不需完整實作 IObservable < Ticket > 。 TicketFactory 類別會實作它自己的訂閱者法,稱為 TicketSubscribe。 這個方法會當做 訂閱 參數傳遞至 Create 運算子。 TicketSubscribe 會在另一個執行緒上建立連續的票證序列,直到訂閱在從 TicketSubscribe 傳回的 IDisposable 介面上呼叫 Dispose 方法取消為止。 IObserver < Ticket > 會傳遞至 TicketSubscribe。 序列中的每個票證都會藉由呼叫 IObserver < Ticket > 來傳遞。OnNext () 。 針對訂閱執行的觀察者動作會顯示主控台視窗的每個票證。

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Reactive.Linq;

namespace Example
{

  class Program
  {

    static void Main()
    {
      IObservable<Ticket> ticketObservable = Observable.Create((Func<IObserver<Ticket>, IDisposable>)TicketFactory.TicketSubscribe);

      //*********************************************************************************//
      //*** This is a sequence of tickets. Display each ticket in the console window. ***//
      //*********************************************************************************//
      using(IDisposable handle = ticketObservable.Subscribe(ticket => Console.WriteLine(ticket.ToString())))
      {
        Console.WriteLine("\nPress ENTER to unsubscribe...\n");
        Console.ReadLine();
      }
    }
  }


  //***************************************************************************************************//
  //***                                                                                             ***//
  //*** The Ticket class just represents a hypothetical ticket composed of an ID and a timestamp.   ***//
  //***                                                                                             ***//
  //***************************************************************************************************//
  class Ticket
  {
    private readonly string ticketID;
    private readonly DateTime timeStamp;

    public Ticket(string tid)
    {
      ticketID = tid;
      timeStamp = DateTime.Now;
    }

    public override string ToString()
    {
      return String.Format("Ticket ID : {0}\nTimestamp : {1}\n", ticketID, timeStamp.ToString());
    }
  }


  //***************************************************************************************************//
  //***                                                                                             ***//
  //*** The TicketFactory class generates a new sequence of tickets on a separate thread. The       ***//
  //*** generation of the sequence is initiated by the TicketSubscribe method which will be passed  ***//
  //*** to Observable.Create().                                                                     ***//
  //***                                                                                             ***//
  //*** TicketSubscribe() provides the IDisposable interface used to dispose of the subscription    ***//
  //*** stopping ticket generation resources.                                                       ***//
  //***                                                                                             ***//
  //***************************************************************************************************//
  public class TicketFactory : IDisposable
  {
    private bool bGenerate = true;


    internal TicketFactory(object ticketObserver)
    {
      //************************************************************************//
      //*** The sequence generator for tickets will be run on another thread ***//
      //************************************************************************//
      Task.Factory.StartNew(new Action<object>(TicketGenerator), ticketObserver);
    }


    //**************************************************************************************************//
    //*** Dispose frees the ticket generating resources by allowing the TicketGenerator to complete. ***//
    //**************************************************************************************************//
    public void Dispose()
    {
      bGenerate = false;
    }


    //*****************************************************************************************************************//
    //*** TicketGenerator generates a new ticket every 3 sec and calls the observer's OnNext handler to deliver it. ***//
    //*****************************************************************************************************************//
    private void TicketGenerator(object observer)
    {
      IObserver<Ticket> ticketObserver = (IObserver<Ticket>)observer;

      //***********************************************************************************************//
      //*** Generate a new ticket every 3 sec and call the observer's OnNext handler to deliver it. ***//
      //***********************************************************************************************//
      Ticket t;

      while (bGenerate)
      {
        t = new Ticket(Guid.NewGuid().ToString());
        ticketObserver.OnNext(t);
        Thread.Sleep(3000);
      }
    }



    //********************************************************************************************************************************//
    //*** TicketSubscribe starts the flow of tickets for the ticket sequence when a subscription is created. It is passed to       ***//
    //*** Observable.Create() as the subscribe parameter. Observable.Create() returns the IObservable<Ticket> that is used to      ***//
    //*** create subscriptions by calling the IObservable<Ticket>.Subscribe() method.                                              ***//
    //***                                                                                                                          ***//
    //*** The IDisposable interface returned by TicketSubscribe is returned from the IObservable<Ticket>.Subscribe() call. Calling ***//
    //*** Dispose cancels the subscription freeing ticket generating resources.                                                    ***//
    //********************************************************************************************************************************//
    public static IDisposable TicketSubscribe(object ticketObserver)
    {
      TicketFactory tf = new TicketFactory(ticketObserver);

      return tf;
    }
  }
}

以下是範例程式碼的輸出範例。 按 Enter 以取消註冊。

Press ENTER to unsubscribe...

Ticket ID : a5715731-b9ba-4992-af00-d5030956cfc4
Timestamp : 5/18/2011 6:48:50 AM

Ticket ID : d9797b2b-a356-4928-bfce-797a1637b11d
Timestamp : 5/18/2011 6:48:53 AM

Ticket ID : bb01dc7d-1ed5-4ba0-9ce0-6029187792be
Timestamp : 5/18/2011 6:48:56 AM

Ticket ID : 0d3c95de-392f-4ed3-bbda-fed2c6bc7287
Timestamp : 5/18/2011 6:48:59 AM

Ticket ID : 4d19f79e-6d4f-4fec-83a8-9644a1d4e759
Timestamp : 5/18/2011 6:49:05 AM

另請參閱

參考

Observable 類別

建立多載

System.Reactive.Linq 命名空間