Threading help

M

MikeScullion

I have set up this thread so my program doesn't hang while I call a cpu
intensive bit of code:

System.Threading.ThreadStart ThreadEncoderStart = new
System.Threading.ThreadStart(myEncoder.EncodeFromConsole);
System.Threading.Thread Thread_myEncoder = new
System.Threading.Thread(ThreadEncoderStart);
Thread_myEncoder.Name = "myEncoder";
Thread_myEncoder.Priority =
System.Threading.ThreadPriority.BelowNormal;
Thread_myEncoder.Start();

In turn I'm calling this from within a "for loop":
for (int x = 0; x <= lbxVideo.Items.Count -1; x++) {
EncodeThread();
}

My problem is how to I make the threads complete in sequence e.g. first
time around run the thread but make the main thread wait until its
finished before proceeding with the next for statement?

any help would be gratefully appreciated.
 
D

David Browne

I have set up this thread so my program doesn't hang while I call a cpu
intensive bit of code:

System.Threading.ThreadStart ThreadEncoderStart = new
System.Threading.ThreadStart(myEncoder.EncodeFromConsole);
System.Threading.Thread Thread_myEncoder = new
System.Threading.Thread(ThreadEncoderStart);
Thread_myEncoder.Name = "myEncoder";
Thread_myEncoder.Priority =
System.Threading.ThreadPriority.BelowNormal;
Thread_myEncoder.Start();

In turn I'm calling this from within a "for loop":
for (int x = 0; x <= lbxVideo.Items.Count -1; x++) {
EncodeThread();
}

My problem is how to I make the threads complete in sequence e.g. first
time around run the thread but make the main thread wait until its
finished before proceeding with the next for statement?

Have the main thread run:

System.Threading.Thread.Join(Thread_myEncoder);

This will block the main thread until the target thread finishes.

David
 
J

Jon Skeet [C# MVP]

If you make the main thread wait until it's finished (which you could
so with Thread.Join) you won't get any benefit from threading.

You might want to look at a producer/consumer queue, where one thread
is always on hand to get requests for encoding, and the producer (the
main thread in this case) can put items on the work queue without
blocking.

See http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml for some
sample code for that (half way down).

Alternatively, your secondary thread could use Control.Invoke to notify
the main thread (which I'm assuming is the UI thread) to tell it that
it has finished, and let the UI respond to that by creating a new
thread.

By the way, it's not clear from the code above, but if your new thread
is trying to look at the control's Items property, it really shouldn't
- see
http://www.pobox.com/~skeet/csharp/threads/winforms.shtml

Jon
 
J

Jon Skeet [C# MVP]

Have the main thread run:
System.Threading.Thread.Join(Thread_myEncoder);
This will block the main thread until the target thread finishes.

If you're going to do that though, you might as well run the code in
the main thread to start with.

Jon
 
C

Cool Guy

In turn I'm calling this from within a "for loop":
for (int x = 0; x <= lbxVideo.Items.Count -1; x++) {
EncodeThread();
}

My problem is how to I make the threads complete in sequence e.g. first
time around run the thread but make the main thread wait until its
finished before proceeding with the next for statement?

Have the worker object asynchroneously raise an event to say that it's
finished, perhaps, and start the next thread once this event has been
raised.

Or create a new thread whose purpose is to run the above 'for' loop.
 
C

Christof Nordiek

Cool Guy said:
Waiting for each thread to finish with Thread.Join, that is.
Or simply do the work inside a loop without further threading.
If all the single jobs have to be done one after another, there is no need
to have an
own thread for each. Simply seperate workerthread from mainthread. That
seemse
to be the task.
 
C

Cool Guy

Christof Nordiek said:
Or simply do the work inside a loop without further threading.
If all the single jobs have to be done one after another, there is no need
to have an
own thread for each.

D'oh. What was I thinking. =/
 
C

Cool Guy

Christof can you explain a little further??

Basically he means use a *single* worker thread to do the whole lot.

i.e.:

(in worker thread) {
loop {
DoBlockingOperation();
}
}
 

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

Top