Solved

Need an ASP.Net Multithreading Example

Posted on 2007-11-16
4
2,437 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
Comment Utility
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
Comment Utility
That will be helpful, but I still need the multithreading example.
0
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now