Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I have a thread as below:

C#
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;
          }

     }
}


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;

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
C#
WaitHandle.WaitAll(signalArray,100,false)
, but the compiler shows error, as WaitAll only used in MTAThread. So I changed the Main() attribute from
C#
[STAThread]
to
C#
[MTAThread] 
(my coding environment is VS C# 2005). But as I have used some 3rd party ActiveX control, which can't be used under
C#
[MTAThread]
, and I had to give up
C#
WaitHandle.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:
C#
Interlock.Exchange(icKillAllThreads,1);

so you can see I did a check for icKillAllThreads value in above code.
Seems I know little about the 3rd parameters meaning for
C#
public static bool WaitAll(
    WaitHandle[] waitHandles,
    int millisecondsTimeout,
    bool exitContext
    );

, can you show me more about this?

Thank you again.
Posted
Updated 2-Mar-14 23:27pm
v10
Comments
Sergey Alexandrovich Kryukov 1-Mar-14 23:55pm    
I answered, but I wonder: why? By the way, what's wrong with just reading MSDN documentation?
—SA
yuzaihuan 2-Mar-14 21:06pm    
Sorry, not quite understood the MSDN documents.

In simple, I need wait all the signals, but can't use WaitAll(), how to do?
Sergey Alexandrovich Kryukov 2-Mar-14 23:41pm    
Why can't you?
—SA
yuzaihuan 3-Mar-14 5:18am    
Not know how to wait all the signals in an effective way.
Sergey Alexandrovich Kryukov 3-Mar-14 9:20am    
You did not answer why not. What's not effective? When a thread is in a wait state, it does not waste any CPU time, did you know that?
—SA

yuzaihua wrote:
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.
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[^].

—SA
 
Share this answer
 
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900