Re: Closing out threads



On Fri, 31 Aug 2007 20:13:07 -0500, "pedrito" <pixbypedrito at
yahoo.com> wrote:


"Lilith" <lilith@xxxxxxxxx> wrote in message
news:2qbhd3lelr1uj1mqvjinmoco6shoev1mql@xxxxxxxxxx
Maybe I'm making some assumptions about how threads work. I have a
form that, on the press of a button, starts monitoring some SMTP
connections. On occasions I make changes while the thread is running
in the background. To reload the changed options I have to exit the
function the thread is running and restart it. I have an indicator
that tells me the function is exiting. But on restart I get an
exception that, to the best of my understanding, says that the thread
is still running and cannot be restarted.

What is the exception?

ThreadStateException. Thread is running or terminated; it cannot
restart.


So, does the thread keep running after I exit the function? Or is
there something I should be doing to tell the thread to terminate? Do
I need to design the function to run the thread until program's end
but read flags from the UI to stop and reload options?


You can't restart a thread after it has exited. You have to start a new
thread. If you want to keep the same thread running, then what you ought to
do is have some sort of queue that it can access (using some sort of locking
mechanism like lock() or Monitor.Enter()/Monitor.Exit()) and simply have it
periodically check to see if there's information it needs, but don't have it
exit until the app is done.

Please verify this for me then so I know I understand. A given

Thread myThread;

can only be initialized/started once. Even when the function specfied
by StartThread is complete, the myThread reference cannot be used
again?

Alternatively, you can simply let the thread close and fire off a new thread
in its place..

New thread? Using the same reference? I thought that's what I was
doing. But apparently my perception of what is going on is cloudy. Or
are we talking about a different thread reference?

Lilith, the ever confounded

P.S. Multitudes of thanks.

Lilith
.