473,387 Members | 1,578 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,387 software developers and data experts.

Threading architectural question [C#]

Hi! I have several methods of different importance that I’d like to call
asynchronously from Main(). I am going to span these threads but will have to
implement some kind of a mechanism to wait until ALL of them finish. I’ve
seen some code on MSDN that simply puts thread to sleep (Thread.Sleep(1000)).
While it’s acceptable in some cases, I think sleeping is dirty. 1st and
foremost, I do not know how much time a background thread is going to take,
so I’d have to give a rather large number of milliseconds to wait for all
threads to finish. 2nd, I hate writing my own loop where I perform periodic
checks.

So, here’s my question:
Is there a way to invoke a method asynchronously in such a way that I’ll be
notified when it’s finished? Is there anything built-in in .NET ? What
classes? Any thread monitors out there? Is there a way to find out when all
threads are finished ?

Any suggestions/links/code samples/pseudo code are greatly appreciated.

Thank you in advance,

Nov 16 '05 #1
4 1119
There is no built in way to wait for an array of thread to finish, however you could hold each of the threads in an array and write

foreach( Thread t in myThreads)
{
t.Join();
}

there are issues about timing out the wait, etc, but you get the idea.

The other thing you could do is use a thread warapper that exposes a WaitHandle and then use WaitHandle.WaitAll or WaitHandle.WaitAny. I wrote a waitable thread a while back which you can download here

http://staff.develop.com/richardb/do....Threading.zip

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

Hi! I have several methods of different importance that I'd like to call
asynchronously from Main(). I am going to span these threads but will have to
implement some kind of a mechanism to wait until ALL of them finish. I've
seen some code on MSDN that simply puts thread to sleep (Thread.Sleep(1000)).
While it's acceptable in some cases, I think sleeping is dirty. 1st and
foremost, I do not know how much time a background thread is going to take,
so I'd have to give a rather large number of milliseconds to wait for all
threads to finish. 2nd, I hate writing my own loop where I perform periodic
checks.

So, here's my question:
Is there a way to invoke a method asynchronously in such a way that I'll be
notified when it's finished? Is there anything built-in in .NET ? What
classes? Any thread monitors out there? Is there a way to find out when all
threads are finished ?
Nov 16 '05 #2
Mike <Mi**@discussions.microsoft.com> wrote:
Hi! I have several methods of different importance that I?d like to call
asynchronously from Main(). I am going to span these threads but will have to
implement some kind of a mechanism to wait until ALL of them finish.


Just call Join on each of the threads in turn.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

"Peter Duniho" <Np*********@nnowslpianmk.comwrote in message
news:op***************@petes-computer.local...
On Tue, 12 Feb 2008 13:46:17 -0800, Scott Roberts
<sr******@no.spam.here-webworks-software.comwrote:
>[...]
Is it reasonable to expect people to be able to click a link and look at
the code before commenting on it?

Sorry, didn't see the link.
No problem. As far as I know, this newsgroup is full of humans. And as we
all know, humans make mistakes. So why don't we all just cut each other a
little slack?

Feb 12 '08 #4
On Tue, 12 Feb 2008 15:07:17 -0800, Scott Roberts
<sr******@no.spam.here-webworks-software.comwrote:
No problem. As far as I know, this newsgroup is full of humans. And as
we all know, humans make mistakes. So why don't we all just cut each
other a little slack?
Why not? I don't understand the question. Who's not cutting someone some
slack?
Feb 12 '08 #5

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

Similar topics

1
by: Lane LiaBraaten | last post by:
I am working on an app that uses the threading module and Queue.Queue(s) for communicating between the threads. Everything was working on Linux (Redhat 7.3) but when I tried it on WinNT 4.0 it...
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...
0
by: Andrzej | last post by:
I am looking up for book about XML architectural forms. Or good site. Regards Andrzej
77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
11
by: stax | last post by:
Hi, Assuming having a method that calls another method which also calls other methods and so on having a long winded tree of methods. What do I do in case it turns out everything has to be...
2
by: engwar | last post by:
Which starter kit or open source project would you consider well-architected? Or more specifically which has good examples to showing a newbie how to separate presentation/business logic/data...
0
by: =?Utf-8?B?VGlt?= | last post by:
I have written both a client (SoapClient) and server (SoapServer) application using WSE 3.0 with the TCP transport. These are completely standalone applications (the Server runs as a Windows...
7
by: Diego F. | last post by:
Hello. I have a doubt about multi-threading, if it is or not the best way to achieve the following: I have a server application that listens a port through a socket. A client will send messages...
7
by: Jeroen | last post by:
We converted our decently large (13 projects, compiled about 12 mb) VStudio 2003/.Net1.1 project to VStudio 2005/.Net2. My two colleagues have no problem starting the new solution, but I get an...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.