473,383 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Threading help

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.EncodeFromC onsole);
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.

Nov 17 '05 #1
10 1580

<Mi**********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.EncodeFromC onsole);
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
Nov 17 '05 #2
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/t...eadlocks.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/t...winforms.shtml

Jon

Nov 17 '05 #3
> 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

Nov 17 '05 #4
<Mi**********@gmail.com>:
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.
Nov 17 '05 #5
I wrote:
Or create a new thread whose purpose is to run the above 'for' loop.


Waiting for each thread to finish with Thread.Join, that is.
Nov 17 '05 #6
Thanks for the info/links...

I shall look into them now.
thanks again guys.

Nov 17 '05 #7
"Cool Guy" <co*****@abc.xyz> schrieb im Newsbeitrag
news:1w***************@cool.guy.abc.xyz...
I wrote:
Or create a new thread whose purpose is to run the above 'for' loop.


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.
Nov 17 '05 #8
Christof Nordiek <cn@nospam.de>:
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. =/
Nov 17 '05 #9
Christof can you explain a little further??

thanks in advance

Nov 17 '05 #10
<Mi**********@gmail.com> wrote:
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();
}
}
Nov 17 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

65
by: Anthony_Barker | last post by:
I have been reading a book about the evolution of the Basic programming language. The author states that Basic - particularly Microsoft's version is full of compromises which crept in along the...
8
by: Z D | last post by:
Hello, I'm having a strange problem that is probably due to my lack of understanding of how threading & COM Interop works in a WinForms.NET application. Here's the situation: I have a 3rd...
13
by: RCS | last post by:
I have a UI that needs a couple of threads to do some significant processing on a couple of different forms - and while it's at it, update the UI (set textboxes, fill in listviews). I created a...
8
by: Yatharth | last post by:
Hi, I m new to threading and i have successfully runed threading but i could display value on my web page ,but its working in code behind when i see it through debugger,plzzzzzzz help me here...
2
by: Vjay77 | last post by:
In this code: Private Sub downloadBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) If Not (Me.downloadUrlTextBox.Text = "") Then Me.outputGroupBox.Enabled = True...
2
by: hecklar | last post by:
This is my first time posting here, so i apologize if i'm posting in the wrong subgroup or whatever, but here goes... I’m having a problem with threading and events (permissions?) in a VB.net...
7
by: Mike P | last post by:
I am trying to write my first program using threading..basically I am moving messages from an Outlook inbox and want to show the user where the process is up to without having to wait until it has...
0
ammoos
by: ammoos | last post by:
hi friends pls help me.. i got an assignment which i feel very difficult to me.. i dont have more knowledge about multi-threading in .net... the assignment details is below.... pls help me... i...
126
by: Dann Corbit | last post by:
Rather than create a new way of doing things: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html why not just pick up ACE into the existing standard:...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.