Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ThreadPool in Page_Load

Posted on 2004-08-28
3
Medium Priority
?
386 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

972 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