Note that there are some explanatory texts on larger screens.

plurals
  1. POClosing side thread before closing the main form
    primarykey
    data
    text
    <p>I'm making a GUI application with windows forms in Visual Studio .NET 2010 using <code>C#</code>. I have multiple threads, for example:</p> <ol> <li>Thread that is pinging target device to make sure the connection is not lost</li> <li>A thread that requests and receives measurement results from the device</li> <li>A thread that is constantly updating the main form with these results. This thread invokes changes in main form controls using <code>Control.Invoke()</code>. My goal: I want to make sure that form is closed properly when user presses (x) on the main form. That means I want my connection to close, all side threads to terminate etc. I'm trying to use <code>FormClosing</code> event. There I set KillThreads flag to true and wait for side threads to terminate. There is a check in each, something like </li> </ol> <pre class="lang-cs prettyprint-override"><code>if(KillThreads) return; </code></pre> <p>But there is an issue. If I try to wait for all threads to be terminated</p> <pre><code>for(;;) if(ActiveSideThreads == 0) break; closeConnection(); </code></pre> <p>the thread N3 freezes blocked by <code>Invoke()</code> call; Sure it is, because the main gui thread is in infinite loop. So the form closing is delayed forever.</p> <p>If I implement before-close operations as a thread procedure 4, and make a new thread in FromClosing event handler, form closes immediately, before the thread 4 termination, and <code>Invoke()</code> throws an error - t. 4 could not close thread 3 in time. If I add <code>Join(), Invoke()</code> calls are blocked again, and thread 3 is never terminated, so thread 4 waits forever.</p> <p>What should I do? Is there some way to resolve this issue?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload