새 스레드에서 작업을 예약하는 스케줄러를 가져옵니다.
네임스페이스:System.Reactive.Concurrency
어셈블리: System.Reactive(System.Reactive.dll)
Syntax
'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