Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

ThreadPool in Page_Load

Posted on 2004-08-28
3
374 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
ID: 11922118
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
ID: 11924864
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
ID: 11935155
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Deploying to Azure 3 38
ASP.NET MVC -Add authentication 2 29
ASP Repeater control - onl;y show if flag is true 7 15
IEnumerable<T> to a List<T> 8 36
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

791 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