WaitAll for multiple handles on a STA thread is not supported 解决方案
2011-11-14 09:59
465 查看
封装一个MTA的WaitALL
class MtaWaitAllWrapper {
readonly WaitHandle[] waitHandles;
readonly int millisecondsTimeout;
bool waitResult;
Exception error;
MtaWaitAllWrapper(int millisecondsTimeout,
params WaitHandle[] waitHandles) {
if (waitHandles == null) throw new ArgumentNullException(@"waitHandles");
this.waitHandles = waitHandles;
this.millisecondsTimeout = millisecondsTimeout;
}
void WaitAll() {
if ((waitHandles.Length > 1) && (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)) {
Thread thread = new Thread(new ThreadStart(OnWaitAll));
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
thread.Join();
} else {
OnWaitAll();
}
}
void OnWaitAll() {
try {
waitResult = WaitHandle.WaitAll(waitHandles, millisecondsTimeout, false);
} catch (Exception ex) {
error = ex;
}
}
public static bool WaitAll(int millisecondsTimeout,
params WaitHandle[] waitHandles) {
MtaWaitAllWrapper waitAllWrapper =
new MtaWaitAllWrapper(millisecondsTimeout, waitHandles);
waitAllWrapper.WaitAll();
if (waitAllWrapper.error != null) throw waitAllWrapper.error;
return waitAllWrapper.waitResult;
}
}
class MtaWaitAllWrapper {
readonly WaitHandle[] waitHandles;
readonly int millisecondsTimeout;
bool waitResult;
Exception error;
MtaWaitAllWrapper(int millisecondsTimeout,
params WaitHandle[] waitHandles) {
if (waitHandles == null) throw new ArgumentNullException(@"waitHandles");
this.waitHandles = waitHandles;
this.millisecondsTimeout = millisecondsTimeout;
}
void WaitAll() {
if ((waitHandles.Length > 1) && (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA)) {
Thread thread = new Thread(new ThreadStart(OnWaitAll));
thread.SetApartmentState(ApartmentState.MTA);
thread.Start();
thread.Join();
} else {
OnWaitAll();
}
}
void OnWaitAll() {
try {
waitResult = WaitHandle.WaitAll(waitHandles, millisecondsTimeout, false);
} catch (Exception ex) {
error = ex;
}
}
public static bool WaitAll(int millisecondsTimeout,
params WaitHandle[] waitHandles) {
MtaWaitAllWrapper waitAllWrapper =
new MtaWaitAllWrapper(millisecondsTimeout, waitHandles);
waitAllWrapper.WaitAll();
if (waitAllWrapper.error != null) throw waitAllWrapper.error;
return waitAllWrapper.waitResult;
}
}
相关文章推荐
- C# 编写Windows Service 调试(WaitAll for multiple handles on a STA thread is not supported.)
- WaitAll for multiple handles on a STA thread is not supported. (原)
- C#等待多线程报错WaitAll for multiple handles on a STA thread is not supported
- iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方案
- iOS iOS8中 问题"registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later" 解决方案
- Android Studio添加应用作为依赖时报错Error:Dependency MonthText:xlistview:unspecified on project app resolves to an APK archive which is not supported as a compilation dependency. File: 及其解决方案
- Java - Why wait, notify and notifyAll is defined in Object Class and not on Thread class
- 综合错误:Assignment under multiple single edges is not supported for synthesis
- Annotation processing is not supported for module cycles解决方案
- windows7 pip *.whl is not a supported wheel on this platform解决方案
- Android Studio 报错:unspecified on project app resolves to an APK archive which is not supported解决方案
- UnsupportedWheel: matplotlib.xxx.whl is not a supported wheel on this platform解决方案
- #Redis Error #MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details a
- HTTP method POST is not supported by this URL解决方案
- Theserver is not reeady for publishing.Please check if the Publishing Tools on the serverare started
- registerForRemoteNotificationTypes: is not supported in iOS 8.0 and later
- 关于Scope 'session' is not active for the current thread的问题
- Android Studio解决unspecified on project app resolves to an APK archive which is not supported
- Unable to start debugging on the web server. The web server is not configured correctly. See help for common configuration errors. Running the web page outside of the debugger may provide further information.
- [ISSUE]Lambda binding for lua is not supported.