Solved

ThreadPool problem

Posted on 2004-08-30
7
562 Views
Last Modified: 2012-08-13
Hi Experts,

I have a threadpool in my method queueing 3 methods:

private mymethod...
{
something to do before

ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod1));
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod2));
ThreadPool.QueueUserWorkItem(new WaitCallback(MyMethod3));

something to do after
}

An exception is thrown because I must wait for those threads to be finished to "do something after".

Could you help me ?

Thanks in advance for your answers.
0
Comment
Question by:noulouk
  • 5
  • 2
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11932600
sorry I missed your comment on the other question http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21110775.html

you would use the manual reset event object http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmanualreseteventclasstopic.asp you would clear them ... then in your callbacks set the signal as complete ... in your page load, wait on the signals ... there is an example in the help that should help clear things up.
0
 
LVL 9

Author Comment

by:noulouk
ID: 11933345
I have always the same trouble (I'm new in threading)
Here is my code:

private void ThreadAll()
        {
            autoEvents = new AutoResetEvent[] {
                                                  new AutoResetEvent(false),
                                                  new AutoResetEvent(false),
                                                  new AutoResetEvent(false)};

            ThreadPool.QueueUserWorkItem(new WaitCallback(method1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(method2));
            ThreadPool.QueueUserWorkItem(new WaitCallback(method3));

            WaitHandle.WaitAll(autoEvents);
        }

private void method1(object stateInfo)
        {
            ...

            autoEvents[0].Set();
        }

private void method2(object stateInfo)
        {
            ...

            autoEvents[1].Set();
        }

private void method3(object stateInfo)
        {
            ...

            autoEvents[2].Set();
        }

An exception is thrown and there is no code available for debugging my webpage !
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
ID: 11933500
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Threading;
namespace WebApplication2
{
      /// <summary>
      /// Summary description for WebForm1.
      /// </summary>
      public class WebForm1 : System.Web.UI.Page
      {
            private ManualResetEvent [] autoEvents;
            private void ThreadAll() {
                  autoEvents = new ManualResetEvent[] {
                                                                          new ManualResetEvent(false),
                                                                          new ManualResetEvent(false),
                                                                          new ManualResetEvent(false)};

                  ThreadPool.QueueUserWorkItem(new WaitCallback(method1));
                  ThreadPool.QueueUserWorkItem(new WaitCallback(method2));
                  ThreadPool.QueueUserWorkItem(new WaitCallback(method3));
                  this.Response.Write("Waiting for threads to complete " + DateTime.Now.ToString() + "\n" );
                  WaitHandle.WaitAll(autoEvents);
                  this.Response.Write("Threads completed " + DateTime.Now.ToString() + "\n" );
            }

            private void method1(object stateInfo) {
                        Thread.Sleep(3000);
                        autoEvents[0].Set();
            }

            private void method2(object stateInfo) {
                        Thread.Sleep(1000);
                        autoEvents[1].Set();
            }

            private void method3(object stateInfo) {
                        Thread.Sleep(5000);
                        autoEvents[2].Set();
            }
            private void Page_Load(object sender, System.EventArgs e) {
                  this.ThreadAll();
            }
            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Load += new System.EventHandler(this.Page_Load);
            }
            #endregion
      }
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11933508
you can catch the exception as a global excepton ...
0
 
LVL 9

Author Comment

by:noulouk
ID: 11934974
Thanks, do you want me to grade you more in ASP .Net or I can delete the question ?
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11935018
honestly points and a dollar will get me a cup of coffee ... glad you got it :)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11935174
heh I guess my its ok didnt work:)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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