Scheduler.NewThread プロパティ

新しいスレッドで作業をスケジュールするスケジューラを取得します。

Namespace:System.Reactive.Concurrency
アセンブリ: System.Reactive (System.Reactive.dll)

構文

'Declaration
Public Shared ReadOnly Property NewThread As NewThreadScheduler
    Get
'Usage
Dim value As NewThreadScheduler

value = Scheduler.NewThread
public static NewThreadScheduler NewThread { get; }
public:
static property NewThreadScheduler^ NewThread {
    NewThreadScheduler^ get ();
}
static member NewThread : NewThreadScheduler
static function get NewThread () : NewThreadScheduler

プロパティ値

種類: System.Reactive.Concurrency.NewThreadScheduler
新しいスレッド スケジューラ。

解説

NewThread スケジューラは、新しいスレッドに対する操作の実行をスケジュールします。 このスケジューラは、実行時間の長い操作に最適です。

このコード例では、3 秒以内にランダムにメールを生成する、IEnumerable から無限の電子メール シーケンスを生成します。 電子メールには、IObservable.TimeStamp 演算子を使用してタイムスタンプが付けられます。 その後、10 秒の期間内に発生するすべての電子メールを保持するバッファーにバッファーされます。 バッファーされたシーケンスのサブスクリプションが作成されます。 最後に、電子メールの各グループが、電子メールに対して生成された対応するタイムスタンプと共にコンソール ウィンドウに書き込まれます。 この例では、タイマーを使用して、電子メール バッファーから配信されるメールの頻度を制御します。 このタイマーは、メインスレッドがキーを押すのを待ってブロックされるため、別のスレッド上に存在します。

using System;
using System.Collections.Generic;
using System.Threading;
using System.Reactive.Linq;
using System.Reactive;

namespace Example
{

  class Program
  {
    static void Main()
    {
      //************************************************************************************************************************//
      //*** By generating an observable sequence from the enumerator, we can use Rx to push the emails to an email buffer    ***//
      //*** and have the buffer dumped at an interval we choose. This simulates how often email is checked for new messages. ***//
      //************************************************************************************************************************//
      IObservable<string> myInbox = EndlessBarrageOfEmails().ToObservable();


      //************************************************************************************************************************//
      //*** We can use the Timestamp operator to additionally timestamp each email in the sequence when it is received.      ***//
      //************************************************************************************************************************//
      IObservable<Timestamped<string>> myInboxTimestamped = myInbox.Timestamp();


      //******************************************************************************************************************************//
      //*** The timer controls the frequency of emails delivered from the email buffer. This timer will be on another thread since ***//
      //*** the main thread will be blocked waiting on a key press.                                                                ***//
      //******************************************************************************************************************************//
      System.Reactive.Concurrency.IScheduler scheduleOnNewThread = System.Reactive.Concurrency.Scheduler.NewThread;


      //***************************************************************************************************************************//
      //*** Create a buffer with Rx that will hold all emails received within 10 secs and execute subscription handlers for the ***//
      //*** buffer every 10 secs.                                                                                               ***//
      //*** Schedule the timers associated with emptying the buffer to be created on the new thread.                            ***//
      //***************************************************************************************************************************//
      IObservable<IList<Timestamped<string>>> newMail = myInboxTimestamped.Buffer(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10), 
                                                                                  scheduleOnNewThread);


      //******************************************************//
      //*** Activate the subscription on a separate thread ***//
      //******************************************************//
      IDisposable handle = newMail.SubscribeOn(scheduleOnNewThread).Subscribe(emailList =>
      {
        Console.WriteLine("\nYou've got mail!  {0} messages.\n", emailList.Count);
        foreach (Timestamped<string> email in emailList)
        {
          Console.WriteLine("Message   : {0}\nTimestamp : {1}\n", email.Value, email.Timestamp.ToString());
        }
      });

      Console.ReadLine();
      handle.Dispose();
    }



    //*********************************************************************************************//
    //***                                                                                       ***//
    //*** This method will continually yield a random email at a random interval within 3 sec.  ***//
    //***                                                                                       ***//
    //*********************************************************************************************//
    static IEnumerable<string> EndlessBarrageOfEmails()
    {
      Random random = new Random();

      //***************************************************************//
      //*** For this example we are using this fixed list of emails ***//
      //***************************************************************//
      List<string> emails = new List<string> { "Email Msg from John ", 
                                               "Email Msg from Bill ", 
                                               "Email Msg from Marcy ", 
                                               "Email Msg from Wes "};

      //***********************************************************************************//
      //*** Yield an email from the list continually at a random interval within 3 sec. ***//
      //***********************************************************************************//
      while (true)
      {
        yield return emails[random.Next(emails.Count)];
        Thread.Sleep(random.Next(3000));
      }
    }
  }
}

コード例からの出力例を次に示します。

You've got mail!  6 messages.

Message   : Email Msg from John
Timestamp : 5/16/2011 3:45:09 PM -04:00

Message   : Email Msg from Wes
Timestamp : 5/16/2011 3:45:12 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:13 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:15 PM -04:00


You've got mail!  7 messages.

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:17 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:18 PM -04:00

Message   : Email Msg from Wes
Timestamp : 5/16/2011 3:45:19 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:21 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:24 PM -04:00

Message   : Email Msg from Bill
Timestamp : 5/16/2011 3:45:26 PM -04:00

Message   : Email Msg from Marcy
Timestamp : 5/16/2011 3:45:26 PM -04:00

参照

リファレンス

Scheduler クラス

System.Reactive.Concurrency 名前空間