Re-running an already created thread.

R

Robert Wilson

Hi,
I am running a separate thread in order to be able to poll devices on a
MODBUS RTU network. I create a thread and look at the ThreadObj.IsAlive
flag, and this work ok. I then might have to come back later to the
thread an re run the task contained within it. When I come to re-run
the thread, when I call to ThreadObj.Start it throws an exception.
Ordinarily in C++ I would delete the ThreadObj and then re-create one
with the oThread = new Thread(new ThreadStart(SearchThread)); call.
Seeing as I cannot do my own garbage collection when I need to wondered
if anyone had any comments?

Most of my experience is C++/MFC and am now porting a current app to C#
in order to better my understanding of the langauge.


Thanks,

Rob.
 
B

Brian Gideon

Robert,

You will have to recreate the thread exactly as you indicated. Though,
you should make sure that the original thread is not running first.
Take a look at the following multipage article on multithreading in
..NET for tips on working with threads. The article is an excellent
resource that is frequently cited in this group.

<http://www.yoda.arachsys.com/csharp/threads/>

Also, there may be better design patterns for executing this task. One
alternative that comes to mind is to create a thread that is dedicated
to executing the task. This thread will sit idle in an infinite loop
waiting for an event (AutoResetEvent or ManualResetEvent) to be
signaled that instructs the thread to begin executing the task. If the
task completes quickly the ThreadPool may be a better approach. It's
difficult to say for sure which one is better without knowing what the
task does.

Brian
 
R

Robert Wilson

Brian said:
Robert,

You will have to recreate the thread exactly as you indicated. Though,
you should make sure that the original thread is not running first.
Take a look at the following multipage article on multithreading in
.NET for tips on working with threads. The article is an excellent
resource that is frequently cited in this group.

<http://www.yoda.arachsys.com/csharp/threads/>

Also, there may be better design patterns for executing this task. One
alternative that comes to mind is to create a thread that is dedicated
to executing the task. This thread will sit idle in an infinite loop
waiting for an event (AutoResetEvent or ManualResetEvent) to be
signaled that instructs the thread to begin executing the task. If the
task completes quickly the ThreadPool may be a better approach. It's
difficult to say for sure which one is better without knowing what the
task does.

Brian
Thanks a lot Brian. Looks like i'm on my way now.

Regards,

Rob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top