Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>Accessing a control from different threads</h2> <p>In <code>WinForms App</code> you can ony access directly a <code>Control</code> from the thread it was created. To do such a task you will need to use <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invokerequired.aspx" rel="nofollow">InvokeRequired</a> property of a control to see if you must use <a href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.invoke.aspx" rel="nofollow">Invoke</a> inorder to force a call of the action from the original thread.<br><br> A public method that might be accessed from any thread including the original would look like this:</p> <pre><code>public void run() { if (label1.InvokeRequired) //Is this method being called from a different thread this.Invoke(new MethodInvoker(()=&gt; label1.Text = CONNECTING)); else //it's cool, this is the original thread, procceed label1.Text = CONNECTING; } </code></pre> <p><br> But if you are absolutly sure that <code>run()</code> method will be called only from the thread, consider not even checking if <code>InvokeRequired</code> and immediatly call <code>Invoke</code><br><br> Further information: <a href="http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms171728(v=vs.80).aspx</a></p> <h2>Stopping a thread in progress</h2> <ul> <li><p>Simples is to use <a href="http://msdn.microsoft.com/en-us/library/system.threading.thread.abort.aspx" rel="nofollow">t1.Abort();</a> method of a <code>Thread</code>. This will throw and exception forcing it to stop where ever it was. this is great for threads that do not do any long processing so stopping it won't cause any problems.</p></li> <li><p>If you do do proccesing in your thread, which means you can't just stop it in the middle then I suggest you to use a boolean that will indicate that the thread must cancel ASAP. <br></p></li> </ul> <p> <pre><code>private bool isCancelRequired = false; public void run() { while(true) { //do long processing.. if (isCancelRequired) break; } } </code></pre> <ul> <li>More advanced methods: <a href="http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml" rel="nofollow">http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml</a></li> </ul>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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