473,805 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Threading help

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

System.Threadin g.ThreadStart ThreadEncoderSt art = new
System.Threadin g.ThreadStart(m yEncoder.Encode FromConsole);
System.Threadin g.Thread Thread_myEncode r = new
System.Threadin g.Thread(Thread EncoderStart);
Thread_myEncode r.Name = "myEncoder" ;
Thread_myEncode r.Priority =
System.Threadin g.ThreadPriorit y.BelowNormal;
Thread_myEncode r.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 1620

<Mi**********@g mail.com> wrote in message
news:11******** **************@ g44g2000cwa.goo glegroups.com.. .
I have set up this thread so my program doesn't hang while I call a cpu
intensive bit of code:

System.Threadin g.ThreadStart ThreadEncoderSt art = new
System.Threadin g.ThreadStart(m yEncoder.Encode FromConsole);
System.Threadin g.Thread Thread_myEncode r = new
System.Threadin g.Thread(Thread EncoderStart);
Thread_myEncode r.Name = "myEncoder" ;
Thread_myEncode r.Priority =
System.Threadin g.ThreadPriorit y.BelowNormal;
Thread_myEncode r.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.Threadin g.Thread.Join(T hread_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.Threadin g.Thread.Join(T hread_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**********@g mail.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.xy z> schrieb im Newsbeitrag
news:1w******** *******@cool.gu y.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

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

Similar topics

65
6771
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 language's 30+ year evolution. What to you think python largest compromises are? The three that come to my mind are significant whitespace, dynamic typing, and that it is interpreted - not compiled. These three put python under fire and cause...
8
8221
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 party COM component that takes about 5 seconds to run one of its functions (Network IO bound call). Since I dont want my GUI to freeze
13
371
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 base class for the worker class, and made up some functions/delegates to handle the invoke stuff for the UI and that was fine for a prototype. I rewrote this chunk, broke things out into different classes - but the threading is still the same - and...
8
13496
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 is the code below: i just wana display the simple array value stored in my array variable in my textbox thats it.
2
2249
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 Me.bytesDownloadedTextBox.Text = "" Me.totalBytesTextBox.Text = ""
2
1422
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 Windows application (background service). I’m trying to write an application that processes files, launching a new thread for each file that is dropped into a certain folder. Now, the application works like a charm on my Win2000 machine, but...
7
2379
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 finished. I am trying to follow this example : http://www.codeproject.com/cs/miscctrl/progressdialog.asp But although the messages still get moved, the progress window never does anything. Here is my code in full, if anybody who knows...
0
1880
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 have to submit this assignment today evening... please....... Windows Application to demonstrate the use of Multi-Threading with Synchronous & Asynchronous Threading. Requirements 1) A form with a) a button to initialize threads ...
126
6760
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: http://www.cse.wustl.edu/~schmidt/ACE.html the same way that the STL (and subsequently BOOST) have been subsumed? Since it already runs on zillions of platforms, they have obviously worked most of the kinks out of the generalized threading and processes idea (along with many...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.