Solved

ThreadPool problem

Posted on 2004-08-30
7
564 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
[X]
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
  • 5
  • 2
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11932600
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
ID: 11933345
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
ID: 11933500
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

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

Author Comment

by:noulouk
ID: 11934974
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
ID: 11935018
honestly points and a dollar will get me a cup of coffee ... glad you got it :)
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11935174
heh I guess my its ok didnt work:)
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

624 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