Solved

ThreadPool problem

Posted on 2004-08-30
7
556 Views
Last Modified: 2012-08-13
Hi Experts,

I have a threadpool in my method queueing 3 methods:

private mymethod...
{
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
  • 5
  • 2
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
sorry I missed your comment on the other question http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/ASP_DOT_NET/Q_21110775.html

you would use the manual reset event object http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingmanualreseteventclasstopic.asp you would clear them ... then in your callbacks set the signal as complete ... in your page load, wait on the signals ... there is an example in the help that should help clear things up.
0
 
LVL 9

Author Comment

by:noulouk
Comment Utility
I have always the same trouble (I'm new in threading)
Here is my code:

private void ThreadAll()
        {
            autoEvents = new AutoResetEvent[] {
                                                  new AutoResetEvent(false),
                                                  new AutoResetEvent(false),
                                                  new AutoResetEvent(false)};

            ThreadPool.QueueUserWorkItem(new WaitCallback(method1));
            ThreadPool.QueueUserWorkItem(new WaitCallback(method2));
            ThreadPool.QueueUserWorkItem(new WaitCallback(method3));

            WaitHandle.WaitAll(autoEvents);
        }

private void method1(object stateInfo)
        {
            ...

            autoEvents[0].Set();
        }

private void method2(object stateInfo)
        {
            ...

            autoEvents[1].Set();
        }

private void method3(object stateInfo)
        {
            ...

            autoEvents[2].Set();
        }

An exception is thrown and there is no code available for debugging my webpage !
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 500 total points
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
you can catch the exception as a global excepton ...
0
 
LVL 9

Author Comment

by:noulouk
Comment Utility
Thanks, do you want me to grade you more in ASP .Net or I can delete the question ?
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
honestly points and a dollar will get me a cup of coffee ... glad you got it :)
0
 
LVL 37

Expert Comment

by:gregoryyoung
Comment Utility
heh I guess my its ok didnt work:)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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