Difference between Abort and Suspend of Thread

  • Thread starter Thread starter Franz
  • Start date Start date
F

Franz

I would like to know the difference between Thread.Abort and Thread.Suspend.
Although I have read the MSDN Library, I still don't know the situations of
using them.
For example, I am writing a HTTP or NNTP Client. I created a Thread to
receive the data through TcpClient. The user want to stop the receiving
process whenever the process is completed or not. So, which should I use?
Abort or Suspend?
Thanks.
 
Franz,
Abort terminates a thread. Once a thread is terminated it cannot be revived.

Suspend puts a thread to sleep indefinitely. Another thread can use Resume
to revive the thread.

I don't think I would use either. The UI thread would set an indicator that
the receive thread checks. When the receive thread sees the indicator it
would politely exit what it is doing & clean up any resources that it needs
to. (read save or delete partial information & close files).

Hope this helps
Jay
 
But the download process should be in a worker thread. The UI thread should
response to the user interaction of aborting the download process.
Am I correct?
 
Franz said:
But the download process should be in a worker thread. The UI thread should
response to the user interaction of aborting the download process.
Am I correct?

As Jay pointed out, once the user presses the Cancel button,
the UI thread sets a flag. The worker thread checks this
flag periodically and once it is set to true it terminates.

Markus
 
Franz,
Correct. As Markus stated.

The UI thread sets an indicator, a flag (a static/shared Boolean variable if
you want) that the worker thread checks periodically.

Hope this helps
Jay
 
In this case you would signal the fact that the user wants to stop the
receiving(either through messaging or synchronization objects, WaitHandle
would be a safe bet here) and exit your thread method. The next time you
want to receive data you create a new thread.

Or you can create a single thread at the start of your application and
signal it the start and end of the receiving. But you will not use Suspend
and Resume (as that will not cancel the receiving), you'll just use wait
functions of WaitHandle class.

Jerry
 
Back
Top