• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2452
  • Last Modified:

Need an ASP.Net Multithreading Example

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.
  • 3
1 Solution
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:

    <add address="*" maxconnection="20" />
hismightinessAuthor Commented:
That will be helpful, but I still need the multithreading example.
Ok, whilst I'm sorting an example, here is an article from MS explaining some of the issues with threading.

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;
        Thread thread2 = new Thread(new ThreadStart(Func2));
        thread2.IsBackground = true;
        Thread thread3 = new Thread(new ThreadStart(Func3));
        thread3.IsBackground = true;
        while (thread1.IsAlive || thread2.IsAlive || thread3.IsAlive)
        DateTime EndTime = DateTime.Now;
        TimeSpan Durration = EndTime - StartTime;

    private void Func1()

    private void Func2()

    private void Func3()
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now