Delegate BeginInvoke and ManualResetEvent.WaitOne()

Hello Experts,

Here is an example:

ManualResetEvent waiter;
delegate DoWorkDelegate;

void DoWork()
{
       waiter.WaitOne();
}

void SubMethodnvoke()
{
DoWorkDelegate.BeginInvoke(DoWork);
}

I do something like this when I open Submethods in my app.
I want to know how many submethods I can open.
I think this asynchronous delegate works as a ThreadPool, so if I open more than the pool threads limit (ie 25 threads by default) I will probably have some troubles.

In fact, I don't know how it works in background. In the msdn documentation, ManualResetEvent.WaitOne() blocks the Thread until it receives a signal. But I hope this is not really the case and the Thread is reused in background to run some other jobs because if I have more than 25 waiting threads in the queue my app is dead.
Is ManualResetEvent.WaitOne() really blocking a Thread or is there any job in the ThreadPool to let those waiting Threads not block all others in the queue ?

Thanks in advance for your help.
LVL 9
nouloukAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

photowhizCommented:
That code cannot possibly work; waiter and DoWorkDelegate are uninitialized. You are not saving the AsyncResult from BeginInvoke, nor calling EndInvoke.

Of course WaitOne will block, that is its purpose. If you don't want to block don't call WaitOne.

There is a good introduction to asynchronous methods at http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx.
0
nouloukAuthor Commented:
Sorry, but the problem is not in my code:
void SubMethodnvoke()
{
...
DoWorkDelegate.BeginInvoke(DoWork);
...
}
If you want.

My question is:
Does the machine free a thread in background if you use WaitOne() or Sleep(10000) ?
I can't find any doc and I'm afraid if a thread really sleeps during 10000 and is not free by the system. It seems to me to be a really bad management of the ThreadPool threads, doesn't it ?
If I run more waiting threads than the pool threads limit, my app is dead.

Hope you understand what information I ask.
0
mastooCommented:
The answer is "no"

I think the problem might be more in the approach to using threads.  You generally want to have some overall approach to maintaining an optimal number of concurrent threads.  There would be some ugly problems if a thread were to change context like that during a sleep or wait.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.