Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

The ManualResetEvent Class

c



+ Font mai mare | - Font mai mic



The ManualResetEvent Class

A ManualResetEvent object can possess only one of the two states; signaled (true) or non-signaled (false). The ManualResetEvent class inherits from the WaitHandle class and the ManualResetEvent constructor takes in a parameter that affirms the initial state of the object. The Set() and Reset() methods return a Boolean value indicating whether the change has taken place successfully or not.



The following listing, NETThreadEvents.cs, shows the use of the ManualResetEvent class with a non-signaled state. First we create an object called mansig and give it a value of false. The WaitOne() method will wait until the mansig turns into true or the time value expires. Since the time duration elapsed while waiting, and the value of mansig was not set to true, it stopped blocking and returned with a value of false:

using System;
using System.Threading;

namespace NETThreadEvents



The output from NETThreadEvents with a value of false is:

ManualResetEvent Before WaitOne
ManualResetEvent After WaitOne False

In NETThreadEvents.cs, we construct a ManualResetEvent object with a value of false. The Boolean value false sets the initial state of the ManualResetEvent object to non-signaled. Then we call the WaitOne() method of the base class WaitHandle. The WaitOne() method takes two parameters. The first one is the number of milliseconds for which we want the thread to wait at the WaitOne() method; the thread therefore waits for one second before quitting. The second parameter is the exitContext. If you are already in the synchronization domain for the context and want to exit the synchronization context or if you want to reacquire the synchronization context, you should set this parameter to true.

The program blocks for one second at the WaitOne() method and then quits because of the timeout. The state of the ManualResetEvent is still false, thus the Boolean b returned by WaitOne() is false. Now let's figure out what will happen if we set the state of ManualResetEvent to signaled (true) when we create it:

using System;
using System.Threading;
namespace NETThreadEvents

}
}

The output from NETThreadEvents with a value of true is:

ManualResetEvent Before WaitOne
ManualResetEvent After WaitOne True

By changing initial state of the ManualResetEvent to signaled, the thread does not wait at the WaitOne() method even though we specified the timeout value of 1,000 milliseconds. When the ManualResetEvent was non-signaled in the previous sample, the thread waited for the state to change to signaled, but it timed out after 1,000 milliseconds. The state is already signaled, so the thread has no reason to wait on the WaitOne() method. To change the state of the ManualResetEvent to non-signaled, we have to call the Reset() method of ManualResetEvent, and to change the state to signaled, we have to call the Set() method.

The following listing, ManualReset.cs, shows the usage of the Reset() method, and the next, ManualSet.cs, shows the usage of the Set() method:

using System;
using System.Threading;

namespace ManualReset



The output from ManualReset is:

ManualResetEvent After first WaitOne True
ManualResetEvent After second WaitOne False

In ManualReset, we set the state of the ManualResetEvent object to signaled (True) in its constructor. As a result, the thread does not does not wait at the first WaitOne() method and returns true. Then we reset the state of the ManualResetEvent object to non-signaled (false), so we see that the thread has to wait for five seconds until it times out.

In Manual Set.cs we use the Set() method:

using System;
using System.Threading;

namespace ManualSet



The output from ManualSet is:

Before WaitOne
ManualResetEvent After first WaitOne False
ManualResetEvent After second WaitOne True

In Manual Set, we set the initial state of the ManualResetEvent object to non-signaled (false). As a result, the thread has to wait on the first WaitOne() method. Then we set the state to signaled using the Set() method, and the thread refuses to wait on the second WaitOne() method, and quits.

Just as the WaitOne() method waits for a single event object to become signaled, the WaitAll() method waits for all the event objects to become true or signaled, or it will stay there until the timeout occurs and the WaitAny() method waits for any of the event objects to become true or signaled.



Politica de confidentialitate | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 613
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved