Solved

ThreadPool in Page_Load

Posted on 2004-08-28
3
368 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

16 Experts available now in Live!

Get 1:1 Help Now