如何等待所有信号? [英] How to Wait all the signals?
问题描述
我有一个主题如下:
I have a thread as below:
private void SendResultOutThreadSub()
{
bool ExitThread = false;
while (!ExitThread)
{
//I need wait 8 signals from other 8 threads:
//In [STAThread] attribute, I can't use WaitHandle.WaitAll
//So I use ManualResetEvent.WaitOne(signal[i],-1,true)
//but this thread seems slow response to my "Exit" button in outside,
//which will set icKillAllThreads=1.
//pls tell me how to solve this problem
if(AllTheSignalIsOn)
SendTheResultOut();
// Check if processing must be terminated
if (Interlocked.Equals(icKillAllThreads, 1))
{
// Set exit loop flag
Console.WriteLine("Sending result out thread existed");
ExitThread = true;
}
}
}
你能帮忙吗?如上所示改进我的线程编码?
在此之前,我将提供有关我的问题的更多信息:
我的项目与图像有关处理。我使用了2台摄像机,每台摄像机都抓了8张图像。为了减少整个处理时间,我想在抓取每个图像后对其进行处理。换句话说,我总共需要处理16张图像。
处理后,我需要将2个摄像头结果(2个输出,通过或失败)组合在一起,如11或10, 00,01到其他IO设备。
简单:
Can you help to improve my thread coding as above shown?
Before this, I will provide more info about my question:
My project is related with image processing. and I used 2 cameras, with 8 images grabbed by each camera. To decrease the whole processing time, I want to process each image once it was grabbed. In other words, I need process 16 images in total.
After processed, I need combine 2 cameras result (2 outputs, pass or fail) together, as "11", or "10", "00", "01" to other IO device.
In simple:
Camera1 grab 8 sequence images --> Process each images one by one --> If failed 3 times, then camera1 failed;<br />
Camera2 grab 8 sequence images --> Process each images one by one --> If failed 3 times, then camera2 failed;<br />
Send result output: Camera1 result+Camera2 result together;
以上代码用于发送结果out。
1)我不知道如何提高我的发送结果线程,因为它需要等待所有信号?
2 )以前我想使用
Above code is for send result out.
1)I am not sure how to improve my send result out thread, as it need wait all the signals?
2)Previously I want to use
WaitHandle.WaitAll(signalArray,100,false)
,但编译器显示错误,因为WaitAll仅用于MTAThread。所以我将Main()属性从
, but the compiler shows error, as WaitAll only used in MTAThread. So I changed the Main() attribute from
[STAThread]
更改为
[MTAThread]
(我的编码环境)是VS C#2005)。但由于我使用了一些第三方ActiveX控件,不能在
(my coding environment is VS C# 2005). But as I have used some 3rd party ActiveX control, which can't be used under
[MTAThread]
下使用,我不得不放弃
WaitHandle.WaitAll();
3)我在console / WinForm应用程序中使用WaitAll进行了一些试用,它在正常状态下运行顺畅,但是当我想退出代码时,似乎它对用户输入的响应较少。现在我在表单上添加一个退出按钮,当它被点击时,它会调用:
3)I had some trial with WaitAll in console/WinForm application, it runs smoothly in normal state, but when I want to exit the code, seems it has less response to user input. Now I add an "Exit" button on the form, when it was clicked, it will call:
Interlock.Exchange(icKillAllThreads,1);
所以你可以看到我在上面的代码中检查了icKillAllThreads值。
似乎我了解第3个参数的含义
so you can see I did a check for icKillAllThreads value in above code.
Seems I know little about the 3rd parameters meaning for
public static bool WaitAll(
WaitHandle[] waitHandles,
int millisecondsTimeout,
bool exitContext
);
,你能告诉我更多关于这方面的信息吗? ?
再次感谢你。
, can you show me more about this?
Thank you again.
推荐答案
哦,使用WaitAll()编译器会显示错误:不支持STAThread上多个句柄的WaitAll,但第三方A我在代码中使用的ctive控件仅在STAThread中受支持。我找到了一个链接:http://www.codeproject.com/KB/recipes/WaitHandleExceptions.aspx?display=Print,它可能对我有帮助。我不知道什么时候一个线程处于等待状态,它不会花费任何CPU。谢谢,SA。
Oh, using WaitAll() the compiler will show error: "WaitAll for multiple handles on STAThread is not supported", but the 3rd party Active controls I used in my code only supported in STAThread. I have found a link: http://www.codeproject.com/KB/recipes/WaitHandleExceptions.aspx?display=Print, it may help me. I don't know when a thread in a wait state, it will not cost any CPU. Thank you, SA.
啊,这个问题很容易解决。
您需要从具有MTA单元状态的线程调用此方法。这是如何:创建一个线程,并在启动之前(重要!),从创建线程,调用 SetApartmentState
:
http://msdn.microsoft.com/en-us /library/system.threading.thread.setapartmentstate%28v=vs.110%29.aspx [ ^ ]。
做主(启动)应用程序线程中的相同内容并不总是可行的。例如,WPF不允许这样做。但对于控制台或 System.Windows.Forms
应用程序,这很有可能。这由应用于应用程序入口点方法的属性( Main
)控制: http://msdn.microsoft.com/en-us/library/system.stathreadattribute%28v=vs.110%29.aspx [ ^ ]。
Ah, this problem is easily solved.
You need to call this method from a thread with MTA apartment state. This is how: create a thread, and, before starting it (important!), from the creating thread, call SetApartmentState
:
http://msdn.microsoft.com/en-us/library/system.threading.thread.setapartmentstate%28v=vs.110%29.aspx[^].
Doing the same from the main (startup) application thread is not always possible. For example, WPF won't allow that. But for a console or a System.Windows.Forms
application this is quite possible. This is controlled by the attribute applied to the entry-point method of your application (Main
): http://msdn.microsoft.com/en-us/library/system.stathreadattribute%28v=vs.110%29.aspx[^].
请参阅:http://msdn.microsoft.com/en-us/library /system.threading.waithandle.waitall(v=vs.110).aspx [ ^ ]
,以防万一: http:/ /msdn.microsoft.com/en-us/library/system.threading.waithandle.waitany(v=vs.110).aspx [ ^ ]。
让我告诉你,有些事让我怀疑你的问题:我不确定你真的需要它;您似乎更有可能要检查线程设计;太糟糕了,你没有分享你活动的目标。
-SA
Please see: http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitall(v=vs.110).aspx[^]
and, just in case: http://msdn.microsoft.com/en-us/library/system.threading.waithandle.waitany(v=vs.110).aspx[^].
Let me tell you that something makes me doubtful about your question: I'm not sure you really need it; it seems to be more likely that you should rather review your threading design; too bad you did not share the goals of your activity.
—SA
这篇关于如何等待所有信号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!