473,326 Members | 2,126 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,326 software developers and data experts.

Threading and Join

Joe
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some help.

Thanks,
Joe
Nov 17 '05 #1
6 1529

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe


How about calling Join after you start all of the threads?

Mythran

Nov 17 '05 #2
Joe,

What you want to do here is call Start on all of your threads first, and
then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe

Nov 17 '05 #3
Joe
Hi Nicholas,

I had tried something similar with a join but the first join doesn't return
until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:O3**************@TK2MSFTNGP09.phx.gbl...
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe


Nov 17 '05 #4

Unfortunately .NET doesn't have a built in semaphore primitive but you can
acheive the same effect with a counter and a ManualResetEvent. The algorithm
below can easily be encapsulated in a Semaphore class {I have one somewhere}:

You will need two variables at a scope that both the main thread and the
worker threads can access:

int numActiveThreads = 0;
ManualResetEvent waitEvent = new ManualResetEvent(false);

Have the MAIN THREAD issue Interlocked.Increment(&numActiveThreads) and then
spin off a worker thread - IN THAT ORDER - increment on main thread/spin off
worker until the desired number of threads have been started. After the
desired number of threads have been started let the main thread sleep on the
unsignalled event.

As each worker thread finishes have it issue
Interlocked.Decrement(&numActiveThreads) and test the result for <= 0. If
<= 0 then the last thread is finishing and you can wake the main thread...

--Richard

"Joe" wrote:
I need to start 8 threads and have the app or calling thread wait until all
threads are done. I was thinking of using Join but that will block the
thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some help.

Thanks,
Joe

Nov 17 '05 #5
Joe,

You are right, the first join doesn't return until the thread is
complete. But that doesn't mean that the second thread stops running.
Also, when you call Join on the second thread, if it completed already, then
the call to Join will return automatically.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Nicholas,

I had tried something similar with a join but the first join doesn't
return until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:O3**************@TK2MSFTNGP09.phx.gbl...
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block
the thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe



Nov 17 '05 #6
Joe
Good point. I didn't think about that.

Thanks again,
Joe

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:Oh**************@tk2msftngp13.phx.gbl...
Joe,

You are right, the first join doesn't return until the thread is
complete. But that doesn't mean that the second thread stops running.
Also, when you call Join on the second thread, if it completed already,
then the call to Join will return automatically.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Hi Nicholas,

I had tried something similar with a join but the first join doesn't
return until the thread is complete.

Thread t = new Thread(new ThreadStart(Test) )
Thread t2 = new Thread(new ThreadStart(Test) )

t.Start();
t2.Start();

t.Join();
t2.Join();

Thanks,
Joe

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote
in message news:O3**************@TK2MSFTNGP09.phx.gbl...
Joe,

What you want to do here is call Start on all of your threads first,
and then call Join on all of the threads after that. For example:

// The threads. Assume they are initialized.
Thread[] threads = ...

// Cycle through the threads and start.
foreach (Thread thread in threads)
{
// Start the thread.
thread.Start();
}

// Now wait on all of them.
foreach (Thread waitThread in threads)
{
// Wait for the thread to complete.
waitThread.Join();
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Joe" <J_no_spam@_no_spam_Fishinbrain.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I need to start 8 threads and have the app or calling thread wait until
all threads are done. I was thinking of using Join but that will block
the thread before any other thread could be started.

I haven't done a lot with threads in C# yet so I could really use some
help.

Thanks,
Joe



Nov 17 '05 #7

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

Similar topics

2
by: Egor Bolonev | last post by:
hi all my program terminates with error i dont know why it tells 'TypeError: run() takes exactly 1 argument (10 given)' =program==================== import os, os.path, threading, sys def...
3
by: Peter Hansen | last post by:
I'm still trying to understand the behaviour that I'm seeing but I'm already pretty sure that it's either a bug, or something that would be considered a bug if it didn't perhaps avoid even worse...
3
by: ron | last post by:
If I use the System.Threading.ThreadPool.QueueUserWorkItem to spawn a new threaded process, will this process be executed regardless of whether or not the current process ends prior to completion...
3
by: Elliot Rodriguez | last post by:
Hi: I am writing a WinForm app that contains a DataGrid control and a StatusBar control. My goal is to update the status bar using events from a separate class, as well as some other simple...
8
by: MattB | last post by:
Hello I am starting a new thread in a button click event. This thread calls an method which sends emails, I don't want the page to wait for the emails to finish going out as it slows the user...
10
by: jt | last post by:
The program works like this: There is a form with a button. When the form is loaded, a separate thread is started which is retreiving/updating data in the database every x seconds. When clicked...
9
by: akrapus | last post by:
Hi, I am trying to understand how to use threading in Python. I get threading as a concept, but not the implementation. In order to start threading, do you call it as a separate function,...
5
by: half.italian | last post by:
Hi all, I don't really understand how to properly use threading in my programs, however I have managed to get by so far using them improperly. Once again I have come up to what I think is...
2
by: Daniel | last post by:
I have a class similar to this: class MyThread(threading.Thread): def __init__(self): self.terminated = False def run(self): while not self.terminated:
2
by: Abandoned | last post by:
Hi.. I want to threading but i have a interesting error.. ========== class SelectAll(threading.Thread): def __init__(self, name): threading.Thread.__init__(self) self.name = name #kelime def...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...

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.