Threading architectural question [C#]

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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,
 
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/dotnet/DM.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 ?
 
Mike said:
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.
 
Hello Richard ,

I have one question, Does your solution handle the following situation ?
One of the events let's say evt1 = new NameableAutoResetEvent(false,
"Local\\StartThread1");
Has been set in one thread, now I want to know in other thread if this event
has been set before this point ,
Can I know that ? (what I mean is ,what is happen when one of the threads
get the Set point before the other one get the initialization of the same
event name ?)



Main thread
{
NameableAutoResetEvent evt1 = new NameableAutoResetEvent(false,
"Local\\StartThread1");
Thread1.start
NameableAutoResetEvent evt2 = new NameableAutoResetEvent(false,
"Local\\StartThread2");
Thread2.start

User click on stopButton
{
evt1.Set()
//Here thread1 already set the Local\\EndThread1 event, Do you have any way
to get the last "Local\\EndThread1"
//Without create new event ?
NameableAutoResetEvent evt1End = new NameableAutoResetEvent(false,
"Local\\EndThread1");
evt1End.WaitOne();

//in this situation I can init the event before the set
NameableAutoResetEvent evt2End = new NameableAutoResetEvent(false,
"Local\\EndThread2");
evt2.Set()
Evt2End.WaitOne();

}

}

Thread1
{
Void start()
{
NameableAutoResetEvent evt = new NameableAutoResetEvent(false,
"Local\\StartThread1");
while(!evt.WaitOne(100,true))
{
Do somthng
}
NameableAutoResetEvent evtEnd = new NameableAutoResetEvent(false,
"Local\\EndThread1");
evtEnd.Set();
}
Thread2
{
Void start()
{
NameableAutoResetEvent evt = new NameableAutoResetEvent(false,
"Local\\StartThread2");
while(!evt.WaitOne(100,true))
{
Do somthng
}
NameableAutoResetEvent evtEnd = new NameableAutoResetEvent(false,
"Local\\EndThread2");
evtEnd.Set();
}
 
Hello Richard ,

I have one question, Does your solution handle the following situation?
One of the events let's say evt1 = new NameableAutoResetEvent(false,
"Local\\StartThread1");
Has been set in one thread, now I want to know in other thread if this
event
has been set before this point ,
Can I know that ? (what I mean is ,what is happen when one of the threads
get the Set point before the other one get the initialization of the same
event name ?)

First, some advice: you _really_ should pay attention to the date on
threads you are replying to. The thread you're replying to here appears
to be over three years old. For all anyone knows, Richard isn't even
reading this newsgroup any more, and in any case, three years is way too
long to have any expectation that anyone will actually remember the thread.

In a situation like this, you should start a new thread, optionally
referencing the original thread by URL if you want to indicate some
relevance of that thread to your own question.

Now, secondly...what's "NameableAutoResetEvent"? It doesn't show up in a
search of MSDN or Google (web or groups). And I don't see anything in
your post that defines it. I don't think you can expect people to give
you advice about some class that isn't known to those people.

Finally, what you can do is going to be strictly limited by the class
you're using. I can't comment on the "NameableAutoResetEvent" class (see
above), but generally speaking there aren't synchronization classes in
..NET that would do what you're asking about automatically. It would need
to be a part of the "NameableAutoResetEvent" class that you're apparently
using.

Possibly such a class would be based on the EventWaitHandle class, which
allows creation of named event objects. But even there, the code would
require that the status of the event, if any, is checked before creatinga
new one, and to avoid race conditions (for example, having the event
status change just after you've checked it but before the new instance of
the named handle is created) you would need to synchronize any access to
the named event (in other words, you'd need a wrapper class to handle all
of the actual work and which would prevent multiple threads from accessing
state simultaneously).

As you can see, it can get very complicated. Generally speaking, it would
be much better to design the code so that it doesn't actually care whether
the state of the event was set prior to attempt to create a new instance..
The code should only care about what the state of the event is "now", not
what the state was at some previous point in time.

Pete
 
Now, secondly...what's "NameableAutoResetEvent"? It doesn't show up in a
search of MSDN or Google (web or groups). And I don't see anything in
your post that defines it. I don't think you can expect people to give
you advice about some class that isn't known to those people.

Is it reasonable to expect people to be able to click a link and look at the
code before commenting on it?
 
[...]
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. However, the link was in the quoted text,
three years old. I can't say that I feel particularly embarassed about
not noticing it, given the relative lack of context provided by the recent
poster.

If for some reason I _am_ supposed to be embarassed about it, I will take
that to mean that the most recent poster expects only the original author
of the class to reply. In that case, I believe it would have been more
appropriate to contact the author directly rather than reviving a
three-year-old thread but regardless that would mean that the person
doesn't expect input from anyone else.

So, duly noted I'll let Richard take over, if and when he may.

Pete
 
Peter Duniho said:
[...]
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?
 
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?
 

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

Back
Top