Solved

ThreadPool in Page_Load

Posted on 2004-08-28
3
362 Views
Last Modified: 2011-09-20
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.
0
Comment
Question by:noulouk
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
Comment Utility
im not sure I am understanding you ... are you saying that you need to wait for these items to finish before  you continue ? If so you could use locking or signalling to handle that case.

0
 
LVL 9

Author Comment

by:noulouk
Comment Utility
Hi gregoryyoung,

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

Could you post me some code to do the job ?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now