Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, this is very wrong. You should never try to manually control a <code>ThreadPool</code> thread. If you need this sort of control, you should be using your own <code>Thread</code> object. In addition, <code>Abort()</code> is not the recommended way of ending a thread; you should have a control <code>volatile bool</code> on your form that the code in <code>MethodToCall</code> checks at various points and exits gracefully when it's <code>true</code>. While you can use the same approach with the <code>ThreadPool</code>, the fact that you need to be able to cancel seems to indicate that the process is long-running, or at least has the potential to be. The <code>ThreadPool</code> shouldn't be used for long-running processes.</p> <p>For example...</p> <pre><code>private volatile bool stopThread = false; private Thread workThread; private void StartThread() { if(workThread == null) { stopThread = false; workThread = new Thread(new ThreadStart(MethodToCall)); workThread.Start(); } } private void StopThread() { if(workThread != null) { stopThread = true; workThread.Join(); // This makes the code here pause until the Thread exits. workThread = null; } } </code></pre> <p>Then in <code>MethodToCall</code>, just check the <code>stopThread</code> boolean at frequent intervals and do any cleanup work that you need to do and exit the method. For example...</p> <pre><code>private void MethodToCall() { // do some work here and get to a logical stopping point if(stopThread) { // clean up your work return; } // do some more work and get to another stopping point if(stopThread) { // clean up your work return; } } </code></pre> <p>And just repeat that pattern.</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.
    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