Solved

Need an ASP.Net Multithreading Example

Posted on 2007-11-16
4
2,438 Views
Last Modified: 2008-02-01
I have a situation where I have a class that goes out and queries several web services.  Now, I have production classes in place that can already call these web services, but I want them all to begin at relatively the same time.  Also, I do not want to continue to the next step until each call has completed.  

I know I can and should do this using an approach of multiple threads, but I cannot seem to get the thread process nailed down.  I first tried to apply an example I found online for pooled threads, and I just tried using an example applying WaitAllEvents.  I am certainly missing something, and this is (obviously) my first time dealing with threads in this fashion.

What I need is a generic, but self-explanatory example on how to start a handful of threads in a method, and then wait for all of them to finish before proceeding.  I would prefer to not see links to sites that I have likely already visited.  Your help is greatly appreciated.
0
Comment
Question by:hismightiness
  • 3
4 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 20301313
I don't have any examples handy but one thing you ought to note is only two outbound (Web Service) calls targeting a given IP address can be pending at a time unless you increase the runtime's default maxconnection setting:

<system.net>
  <connectionManagement>
    <add address="*" maxconnection="20" />
  </connectionManagement>
</system.net>
0
 
LVL 9

Author Comment

by:hismightiness
ID: 20301333
That will be helpful, but I still need the multithreading example.
0
 
LVL 16

Expert Comment

by:McExp
ID: 20301454
Ok, whilst I'm sorting an example, here is an article from MS explaining some of the issues with threading.

http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/
0
 
LVL 16

Accepted Solution

by:
McExp earned 500 total points
ID: 20301614
The code below should demonstrate the principals of Threaded processing: -

The code completes all 3 functions (total duration  3.5 seconds) and you should see that the StartTime and EndTime will be about 2 seconds apart (give or take ~100ms)

    protected void Page_Load(Object sender, EventArgs e)
    {
        DateTime StartTime = DateTime.Now;
        Thread thread1 = new Thread(new ThreadStart(Func1));
        thread1.IsBackground = true;
        thread1.Start();
        Thread thread2 = new Thread(new ThreadStart(Func2));
        thread2.IsBackground = true;
        thread2.Start();
        Thread thread3 = new Thread(new ThreadStart(Func3));
        thread3.IsBackground = true;
        thread3.Start();
        while (thread1.IsAlive || thread2.IsAlive || thread3.IsAlive)
        {
            Thread.Sleep(100);
        }
        DateTime EndTime = DateTime.Now;
        TimeSpan Durration = EndTime - StartTime;
    }

    private void Func1()
    {
        Thread.Sleep(500);
    }

    private void Func2()
    {
        Thread.Sleep(2000);
    }

    private void Func3()
    {
        Thread.Sleep(1000);
    }
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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

10 Experts available now in Live!

Get 1:1 Help Now