Threading Question

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Hi all,

I recently ran across some code for a splash page sample at
thecodeproject.com. It's a nice piece of code that gradually fades out and
runs on a separate thread. Apart from the issues with it updating the UI
directly from a worker thread that I was able to spot and fix (thanks to
some previous posts on the subject by Mr. Skeet!), I found something else
odd. To stop the worker thread from running, the programmer used the
statement myThread = null; This doesn't seem right to me, but I just wanted
to be sure before I go changing it. Is this the correct way to stop a
worker thread? Will it have any adverse effects on the system? What about
using Thread.Join() instead? Just wondering...

Thanks,
Michael C., MCDBA
 
Michael C said:
I recently ran across some code for a splash page sample at
thecodeproject.com. It's a nice piece of code that gradually fades out and
runs on a separate thread. Apart from the issues with it updating the UI
directly from a worker thread that I was able to spot and fix (thanks to
some previous posts on the subject by Mr. Skeet!), I found something else
odd. To stop the worker thread from running, the programmer used the
statement myThread = null; This doesn't seem right to me, but I just wanted
to be sure before I go changing it. Is this the correct way to stop a
worker thread? Will it have any adverse effects on the system? What about
using Thread.Join() instead? Just wondering...

Setting myThread = null; will certainly not stop the thread.
Thread.Join() won't stop a thread - it'll make the current thread wait
until the other thread has terminated.

See http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml for a
better way to stop worker threads. Note the last paragraph :)
 
Back
Top