C#等待多线程报错WaitAll for multiple handles on a STA thread is not supported
2013-09-02 16:31
288 查看
在开发winform时,碰到过必须把Main设置成MTA thread方式,但有时却有些程序不支持使用的情况,如剪贴板,于是乎使用以下方法解决
Thread t = new Thread(new ThreadStart(Method));
t.SetApartmentState(ApartmentState.STA);
t.Start();
即通过设置为ApartmentState.STA达到解决目的。
后来却碰到Winform中的AlloDrop设置为True时,出现下以错误
WaitAll for multiple handles on a STA thread is not supported
似乎还是得标识Main为STAThread方式。因此只好寻求反向解决方式,经过一番折腾使用以下办法得到解决,
有如如释重负
private void WaitAll(WaitHandle[] waitHandles) {
if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
// 使用foreach,在多线程中等待每一个句柄
foreach(WaitHandle myWaitHandle in waitHandles) {
WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
}
}
else {
WaitHandle.WaitAll(waitHandles);
}
}
转载 http://www.cnblogs.com/jes_shaw/archive/2009/06/26/1511505.html
Thread t = new Thread(new ThreadStart(Method));
t.SetApartmentState(ApartmentState.STA);
t.Start();
即通过设置为ApartmentState.STA达到解决目的。
后来却碰到Winform中的AlloDrop设置为True时,出现下以错误
WaitAll for multiple handles on a STA thread is not supported
似乎还是得标识Main为STAThread方式。因此只好寻求反向解决方式,经过一番折腾使用以下办法得到解决,
有如如释重负
private void WaitAll(WaitHandle[] waitHandles) {
if (Thread.CurrentThread.ApartmentState == ApartmentState.STA) {
// 使用foreach,在多线程中等待每一个句柄
foreach(WaitHandle myWaitHandle in waitHandles) {
WaitHandle.WaitAny(new WaitHandle[]{myWaitHandle});
}
}
else {
WaitHandle.WaitAll(waitHandles);
}
}
转载 http://www.cnblogs.com/jes_shaw/archive/2009/06/26/1511505.html
相关文章推荐
- 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 解决方案
- WaitAll for multiple handles on a STA thread is not supported. (原)
- 解决c#所有单线程单元(STA)线程都应使用泵式等待基元(如 CoWaitForMultipleHandles),并在运行时间很长的操作过程中定期发送消息。 转载
- C#WebService 出现Access-Control-Allow-Origin header is present on the requested resource无响应解决方法
- Custom ThreadPool in Java to Perform as ThreadPool in C# based on Singleton Model
- c# 演示通过 isThreadPoolThread 来判断是不属于托管线程池
- C#中的Thread中的ApartmentState几种状态(STA,MTA,Unknown)详解
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to th
- 提交页面出现提示:Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effe
- QT: QFileSystemModel _q_fileSystemChanged slot is executed on the UI thread which contradicts docume
- WampDeveloper Pro is the best (and fastest) way to create and run multiple websites on Windows, usin
- C# 调试问题:CoWaitForMultipleHandles
- c# - the calling thread must be STA, because ma...
- Java - Why wait, notify and notifyAll is defined in Object Class and not on Thread class
- 关于谷歌..Synchronous XMLHttpRequest on the main thread is deprecated because of its...
- Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental……
- Javascript is based on signal thread
- 在没有安装office2010的64位系统上C#读取excel内容出现 'Microsoft.Jet.OLEDB.4.0' provider is not registered on the loca
- C# Thread STA模式 与MTA模式