. NET thread Synchronization (3)

Source: Internet
Author: User
Tags bool manual net thread reset semaphore thread
Another synchronization strategy is manual technology, which system.threading some classes in the namespace that can be used for manual synchronization. The ManualResetEvent class is used to keep a thread in a waiting state, which has 2 states: signaled (True) or no signal (False). There are 2 other important methods: Reset () and set ().

The following code illustrates the use of the Reset () method:

Using System;
Using System.Threading;

Namespace Manualreset
{

Class Reset
{

[STAThread]
static void Main ()
{
ManualResetEvent manre;
Manre=new ManualResetEvent (TRUE); Assign to the semaphore
BOOL State=manre.waitone (1000,true);
Console.WriteLine ("ManualResetEvent after the WaitOne" +state);

Manre.reset (); Set ManualResetEvent status to no semaphore
State=manre.waitone (5000,true);
Console.WriteLine ("ManualResetEvent after second WaitOne" +state);
}
}
}

Run Result:



The following code illustrates the use of the Set () method:

Using System;
Using System.Threading;
Namespace Manualset
{

Class Set
{

[STAThread]
static void Main (string[] args)
{
ManualResetEvent manre;
Manre=new ManualResetEvent (FALSE);
Console.WriteLine ("Before WaitOne");
BOOL State=manre.waitone (5000,true);
Console.WriteLine ("ManualResetEvent after the WaitOne" +state);

Manre.set (); Set its state to a semaphore
Thread.Sleep (3000);
State=manre.waitone (5000,true);
Console.WriteLine ("ManualResetEvent after second WaitOne" +state);
}
}
}

Run Result:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.