Introduction to applications of threadpool. registerwaitforsingleobject and waithandle

Source: Internet
Author: User

I recently learned thread-related content, so I have recorded my experiences in the learning process.

Threadpool. queueuserworkitem () or thread thd = new thread (New threadstart (TEST), but few people should know how to use threadpool. registerwaitforsingleobject (except for experts. Let me explain its usage to you. First, let's look at its prototype:

 

Code
Public StaticRegisteredwaithandle registerwaitforsingleobject (
Waithandle waitobject,
Waitortimercallback callback,
Object state,
IntMillisecondstimeoutinterval,
BoolExecuteonlyonce
)

 

Its parameters are described as follows:

Parameters
Waitobject
Type: system. threading . . :: .Waithandle

The waithandle to register. Use a waithandle other than mutex.

Callback
Type: system. threading . . :: .Waitortimercallback

The waitortimercallback delegate to call whenWaitobjectParameter is signaled.

State
Type: System . . :: .Object

The object that is passed to the delegate.

Millisecondstimeoutinterval
Type: System . . :: .Int32

The time-out in milliseconds. IfMillisecondstimeoutintervalParameter is 0 (zero), the function tests the object's state and returns immediately. IfMillisecondstimeoutintervalIs-1, the function's time-out interval never elapses.

Executeonlyonce
Type: System . . :: .Boolean

TrueTo indicate that the thread will no longer wait onWaitobjectParameter after the delegate has been called;FalseTo indicate that the timer is reset every time the wait operation completes until the wait is unregistered.

Return Value

Type: system. threading..::.Registeredwaithandle

The registeredwaithandle that encapsulates the native handle.

I believe that after reading this, we are still confused. This method is used to add a method that can be periodically executed to the thread pool, and the fourth parameter.MillisecondstimeoutintervalIs used to set the interval for execution, but here the fifth ParameterExecuteonlyonceIt will take effect on the fourth parameter. If it is true, it indicates that the task will only be executed once, that is, it will not. It is executed once at a certain time, like timer, this function can also be implemented using the timer control.

This method also provides a semaphore-based method to trigger execution tasks.

A semaphore is also called a switch. Therefore, it is called a semaphore. It has only two States: true or false,

Waithandle is the basic class of this type of switch. Its inherited classes include mutex, manualresetevent, and autoresetevent. Generally, we use the last two

Statement:

 

Code
StaticManualresetevent wait2=NewManualresetevent (False);
StaticAutoresetevent wait=NewAutoresetevent (False);

 

We can specify the initial value of the switch when instantiating it. (True indicates a signal, and false indicates no signal)

The difference between manualresetevent and autoresetevent is:

After the former calls the set method, the switch value is automatically kept to true, and the latter calls the set method to change to true immediately and then to false, which can be understood as a pulse.

Let's look at several examples.

Example 1: implement a common Timer

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Threading;

Namespace Test
{
Class Class14
{
Static Autoresetevent wait = New Autoresetevent ( False );
Static   Void Main ( String [] ARGs)
{
Object State = New   Object ();
Threadpool. registerwaitforsingleobject (wait, New Waitortimercallback (test11), state, 5000 , False );
Console. readkey ();
}

Private Static VoidTest11 (ObjectState,BoolTimedout)
{
Console. writeline ("Aaa");
}
}
}

 

Example 2: When the switch quantity is used to execute the next task in advance, the task is executed immediately (a bit round, that is, the next task is executed at an interval of 10 seconds, but the Set Method of the switch quantity is used, execute the next task immediately. The execution time is earlier and the next execution time starts again.

Pay attention to the following details:CodeWhen we register the task statement threadpool. registerwaitforsingleobject (wait, new waitortimercallback (test11), State, 5000, false) to the thread pool );
It is executed at an interval of 5 seconds. It takes 5 seconds to execute the callback function for the first time after it is added to the thread pool, that is to say, a callback function will not be executed once every 0th seconds. A wait is used here. the Set () method enables immediate execution of the callback function without waiting for 5 seconds. Therefore, the first "AAA" output result is generated by wait. set () operation.

If we do not use wait. Set (), but change the initial value of autoresetevent wait = new autoresetevent (true) to true, "AAA" will be output in 0th seconds"

 

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Threading;

Namespace Test
{
Class Class14
{
Static Manualresetevent wait2 = New Manualresetevent ( False );
Static Autoresetevent wait = New Autoresetevent ( False );
Static   Void Main ( String [] ARGs)
{
Object State = New   Object ();
Threadpool. registerwaitforsingleobject (wait, New Waitortimercallback (test11), state, 5000 , False );
Wait. Set ();
Console. readkey ();
}

Private Static VoidTest11 (ObjectState,BoolTimedout)
{
Console. writeline ("Aaa");
}
}
}

Example 3: Use manualresetevent. This example is a bit interesting. It will output "AAA" continuously. Why?

 

Code
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. text;
Using System. Threading;

Namespace Test
{
Class Class14
{
Static Manualresetevent wait = New Manualresetevent ( True );
Static   Void Main ( String [] ARGs)
{
Object State = New   Object ();
Threadpool. registerwaitforsingleobject (wait, New Waitortimercallback (test11), state, 5000 , False );

Console. readkey ();
}

Private Static VoidTest11 (ObjectState,BoolTimedout)
{
Console. writeline ("Aaa");
}
}
}

Manualresetevent always triggers the execution of the callback function because the value of manualresetevent is always set to true,

 

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.