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

x
?
Solved

ThreadPool problem

Posted on 2004-08-30
7
Medium Priority
?
566 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
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 2000 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
Technology Partners: 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

916 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