Problem with AutoResetEvent Array and WaitHandle.WaitAll()
-
I am tryng to split a process in some methods stopping the methods with objects ManualResetEvent and AutoResetEvent but I am having problems with the WaitHandle.WaitAll() method with this method I want stop the proccess until everyone event is pointed, but I receive the error message of "Not valid WaitAll for various events in a STA proccess" Please ho can I solve the problem
-
I am tryng to split a process in some methods stopping the methods with objects ManualResetEvent and AutoResetEvent but I am having problems with the WaitHandle.WaitAll() method with this method I want stop the proccess until everyone event is pointed, but I receive the error message of "Not valid WaitAll for various events in a STA proccess" Please ho can I solve the problem
Use Google. The first hit that comes up already provides a solution:
private void WaitAll(WaitHandle[] waitHandles) {
if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
// WaitAll for multiple handles on an STA thread is not supported.
// ...so wait on each handle individually.
foreach(WaitHandle myWaitHandle in waitHandles) {
WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
}
}
else {
WaitHandle.WaitAll(waitHandles);
}
}Standards are great! Everybody should have one!
-
I am tryng to split a process in some methods stopping the methods with objects ManualResetEvent and AutoResetEvent but I am having problems with the WaitHandle.WaitAll() method with this method I want stop the proccess until everyone event is pointed, but I receive the error message of "Not valid WaitAll for various events in a STA proccess" Please ho can I solve the problem
You can start a new thread, set its apartment state to MTA and call the WaitAll method there. Then just call Join on the thread. It is much better then using a foreach loop.
Giorgi Dalakishvili #region signature My Articles Asynchronous Registry Notification Using Strongly-typed WMI Classes in .NET [^] My blog #endregion