Here is my code where I'm having problem with. It seems like
"ThreadPool.Que ueUserWorkItem( new WaitCallback (PopulatContext UsersTable),
contextUserEven t);
" is being executed before the waiting for the 1st 3 threads are done. Is
there something wrong with my code or it's the problem with the
AutoResetEvent?
Also, would it be better for me to use the BackgrounWorder class? Can this
be used for muliple threads as I need it in my code? Thank you.
private void GenerateDataFor ComputerDetailR eport()
{
AutoResetEvent autoEvent = new AutoResetEvent (false), contextEvent = new
AutoResetEvent (false),
adUserEvent =
new AutoResetEvent( false ), contextUserEven t = new AutoResetEvent( false );
try
{
dsReport.Tables[
"PadCompute rs"].Clear();
dsReport.Tables[
"AdComputer s"].Clear();
switch(reportNa me)
{
case "Computer Detail":
ThreadPool.Queu eUserWorkItem(n ew WaitCallback
(PopulateContex tComputerTable) , contextEvent);
ThreadPool.Queu eUserWorkItem(n ew WaitCallback (PopulatAdCompu tersTable),
autoEvent);
contextEvent.Wa itOne();
autoEvent.WaitO ne();
if (dsReport.Table s["PAdCompute rs"].Rows.Count == 0 ||
dsReport.Tables[
"AdComputer s"].Rows.Count == 0)
return;
CreateComputers Table();
break;
case "Computer Users Detail":
ThreadPool.Queu eUserWorkItem(n ew WaitCallback (PopulatAdUsers Table),
adUserEvent);
ThreadPool.Queu eUserWorkItem(n ew WaitCallback
(PopulateContex tComputerTable) , contextEvent);
ThreadPool.Queu eUserWorkItem(n ew WaitCallback (PopulatAdCompu tersTable),
autoEvent);
contextEvent.Wa itOne();
autoEvent.WaitO ne();
if (dsReport.Table s["PAdCompute rs"].Rows.Count == 0 )
return;
CreateComputers Table();
adUserEvent.Wai tOne();
ThreadPool.Queu eUserWorkItem(n ew WaitCallback (PopulatContext UsersTable),
contextUserEven t);
//Let's get the current computer name
contextUserEven t.WaitOne();
//After we create the Context user table then proceed
CreateComputerU sersTable();
break;
}
//end switch
}
//end try
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
--
Thanks.
"Chris Mullins [MVP]" wrote:
AutoResetEvent there is pretty scary. If your "are you done yet?" thread
isn't waiting already, then it's going to miss the fact that your work item
is complete. This means the order you do things in is very important, and
(worse) is also hard to get right.
You would be better served, I suspect, using a ManualResetEven t. This
eliminates the problem, and makes your code less likley to have race
conditions.
--
Chris Mullins, MCSD.NET, MCPD:Enterprise , MVP C#
http://www.coversant.net/blogs/cmullins
"Pucca" <Pu***@discussi ons.microsoft.c omwrote in message
news:89******** *************** ***********@mic rosoft.com...
I'm using threadpool so I used AutoeventReset and it worked. Thank you
very
much.
--
Thanks.
"Bruce Wood" wrote:
You use the Join method on the threads. However, there's a small
problem: if you call Join in the UI thread, then you hang the UI.
The usual solution is to use one of the two threads as a "controller ",
or use a third thread. In the first case, the second thread to be
started can be handed a reference to the first thread, and do a Join
before it signals that it's finished. If the first thread isn't
finished yet, then the second thread will wait on the Join until it is.
Therefore, when the second (controller) thread is finished, you know
that they're both finishes.
If the two threads are not logically related this way, then it might be
nicer to use a third, controller thread: the UI starts the controller,
which then starts the two worker threads. The controller then does two
Joins, one on each worker thread, and when the two Joins complete, it
signals the UI that all is done. This is scalable to an arbitrary
number of worker threads.
On Jan 26, 10:35 am, Pucca <P...@discussio ns.microsoft.co mwrote:
Thank you Christof. These 2 threads are started within a form as
worker
threads only. They are coded as 2 static method within this form. The
2
threads have no form of its own. How can I then find out when they're
both
done with their tasks?
--
Thanks.
"Christof Nordiek" wrote:
Hi Pucca,
the WaitCallBack-handler (PopulateContex tTable e.g.) should signal
that at
the and, maybe in a finally block. If this is called from a
GUI-Thread you
could call BeginInvoke on one of the Forms methods.
Christof
"Pucca" <P...@discussio ns.microsoft.co mschrieb im Newsbeitrag
>news:E8******* *************** ************@mi crosoft.com...
Hi, I'm using vs2005 and .net 2.0. I started 2 threadpool threads.
How
do I
know when they're done with their tasks? Thanks.
ThreadPool.Queu eUserWorkItem(n ew
WaitCallback(Po pulateContextTa ble));
ThreadPool.Queu eUserWorkItem(n ew WaitCallback(Po pulatAdTable));
--
Thanks.- Hide quoted text -- Show quoted text -