Delegate BeginInvoke and ManualResetEvent.WaitOne()

Posted on 2008-02-11
Medium Priority
Last Modified: 2013-12-17
Hello Experts,

Here is an example:

ManualResetEvent waiter;
delegate DoWorkDelegate;

void DoWork()

void SubMethodnvoke()

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.
Question by:noulouk

Expert Comment

ID: 20869939
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.

Author Comment

ID: 20873705
Sorry, but the problem is not in my code:
void SubMethodnvoke()
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.
LVL 21

Accepted Solution

mastoo earned 2000 total points
ID: 20875588
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.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The PowerShell Core 6.0 of .NET release is just the beginning. The upcoming PowerShell Core 6.1 would have artificial intelligence and internet of things capabilities. So many things to look forward to in the upcoming release.
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Suggested Courses

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question