Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ThreadPool problem

Posted on 2004-08-30
7
Medium Priority
?
565 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

722 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