Wait(int, CancellationToken) Wait(TimeSpan, CancellationToken) IsSet


Set the event, releasing any waiting Tasks. While the event is set, calls to Wait() do not block until the Reset() method is called.

Reset the event.

Calls to this method block until the event is set, the specified time period has passed, or the specified token is cancelled.

Return true if the event is set and false otherwise.

Get and set the number of spins that a call to Wait() will result in before a normal wait occurs. See Chapter 2 for more information about spinning.

■ Note The ManualResetEventSlim class is the lightweight equivalent to System.Threading.ManualResetEvent.

Listing 4-16 demonstrates the use of the ManualResetEventSlim class. Two Tasks are created: one worker that repeatedly waits on the event and one supervisor that sets and unsets the event. While the event is set, calls to the Wait() method do not block, and the worker Task proceeds without waiting. When the event is reset, calls to Wait() block until the supervisor sets the event once again.

■ Tip The default constructor creates an instance of ManualResetEventSlim with the event initially unset, but you can explicitly specify the initial state of the event by using the overloaded version of the constructor.

Listing4-16. Using theManualResetEventSlim Class using System;

using System.Threading;

using System.Threading.Tasks;

namespace Listing_l6 {

class Listing_l6 {

static void Main(string[] args) {

// create the primtive ManualResetEventSlim manualResetEvent = new ManualResetEventSlim();

// create the cancellation token source CancellationTokenSource tokenSource = new CancellationTokenSource();

// create and start the task that will wait on the event Task waitingTask = Task.Factory.StartNew(() => { while (true) {

// wait on the primitive manualResetEvent.Wait(tokenSource.Token); // print out a message Console.WriteLine("Waiting task active");

}, tokenSource.Token);

// create and start the signalling task Task signallingTask = Task.Factory.StartNew(() => { // create a random generator for sleep periods Random rnd = new Random(); // loop while the task has not been cancelled while (ItokenSource.Token.IsCancellationRequested) { // go to sleep for a random period tokenSource.Token.WaitHandle.WaitOne(rnd.Next(500, 2000));

// set the event manualResetEvent.Set();

Console.WriteLine("Event set"); // go to sleep again tokenSource.Token.WaitHandle.WaitOne(rnd.Next(500, 2000));

// reset the event manualResetEvent.Reset();

Console.WriteLine("Event reset");

// if we reach this point, we know the task has been cancelled tokenSource.Token.ThrowIfCancellationRequested(); }, tokenSource.Token);

// ask the user to press return before we cancel // the token and bring the tasks to an end Console.WriteLine("Press enter to cancel tasks"); Console.ReadLine();

// cancel the token source and wait for the tasks tokenSource.Cancel();

Task.WaitAll(waitingTask, signallingTask); } catch (AggregateException) { // discard exceptions

// wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine();

0 0

Post a comment