PDA

Click to See Complete Forum and Search --> : re-using a thread


Techno
Oct 19th, 2007, 04:20 AM
I am wanting to re-use a thread I have created, to do some work.

What is the best way of doing this? I don't want to spin up a new thread each time this "DoWork()" Method is called. I'd rather have the same thread run the same task. It will only be called when a task has finished completing.


Whats happening is this:

My business logic spins up a thread to run a method.
This method is kind of using a for loop, but not quite - basically checking the index stored globally to see if its < than a collection of items. If so, it will do some work.

There will be events firing, and one of them is called the "FinishedWork" event, which the business logic subscribes to. When this event is fired, it will call the method to do more work, if there is more work to be done.

I want to re-use the thread to do that work, to kind of restart that method call which has been set in the Thread.

example:


public void DoStartWork()
{
if (this.t == null)
{
this.t = new Thread(new ThreadStart(DoWork));
}
t.IsBackground = true;
t.Start();
}


//event subscription:

FinishedWork()
{
this.DoWork()
}


DoWork()
{
if (this._indexPosition < someCollection.Count)
{
//do some work
}
}




So basically, in the FinishedWork event, I want it to re-use the existing thread if possible and DoWork() on that thread, as it does the first time.

If it is not possible to restart a stopped thread, what is the best way of disposing of that thread safely without it just "laying" there?

Thanks

zdavis
Oct 19th, 2007, 09:21 AM
I would say that you should try to suspend the thread then restart it or you could tell the Thread to sleep for a certain amount of time.

Techno
Oct 19th, 2007, 09:26 AM
no really, both are very bad practices :)

if the thread has finished, you cant restart it. I am trying to now use ManualResetEvents which seems to work, but still have to spin up threads everytime but at least it doesnt consume the CPU 100% again when creating new threads. still though, would like to know more about this.

Other thing ive noticed is that even the the work is done in a thread, on a certain point (when populating data to an excel sheet using the PIA) the UI thread does hang until I get the event to continue/change status.

seems like i need 2 threads here, one for doing the work and the other to do the work inside the thread that is suppose to be doing work, but dont want this either.


its hard to explain. to try and give you a better view......


a method is called to spin up a thread and do work.
the Do work raises an event to do work.
the "work" area will raise events when its completed a task.
in the class that creates the threads, it has subscribed to these events, so it can notify the UI of the progress.
On the final event (WorkFinished) it will update the UI as well as do a Reset on the ManualResetEvent and also join the current worker thread.

The part that I dont understand is that why the UI thread also hangs when the worker thread is populating data in an Excel spreadsheet using the PIA.

When its populating the data, it is doing it from the worker thread, which raises an event to start populating data.