Link to home
Start Free TrialLog in
Avatar of noulouk
noulouk

asked on

ThreadPool in Page_Load

Hi Experts,

I have a threadpool in Page_Load queueing 3 methods:

private Page_Load ...
{
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.
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of noulouk
noulouk

ASKER

Hi gregoryyoung,

You're right, I want them to finish before continue.

Could you post me some code to do the job ?
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
     }
}