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 when the waitobject parameter is signaled.
-
State
-
Type: system...:. Object
The object that is passed to the delegate.
-
Millisecondstimeoutinterval
-
Type: system...:. int32
The time-out in milliseconds. if the millisecondstimeoutinterval parameter is 0 (zero), the function tests the object's state and returns immediately. if millisecondstimeoutinterval is-1, the function's time-out interval never elapses.
-
Executeonlyonce
-
Type: system...:. Boolean
True to indicate that the thread will no longer wait on the waitobject parameter after the delegate has been called; false to 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 for scheduled execution to the thread pool. The fourth parameter millisecondstimeoutinterval is used to set the interval for execution, however, the fifth executeonlyonce parameter takes effect for the fourth parameter. If it is true, it indicates that the task will only be executed once, that is, it will not. Like timer, the task will be executed once every certain time, 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:
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
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"
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. 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?
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. 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.
From: http://www.cnblogs.com/lexus/archive/2008/08/08/1263718.html