?
Solved

Need an ASP.Net Multithreading Example

Posted on 2007-11-16
4
Medium Priority
?
2,445 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
[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
  • 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
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…
Suggested Courses

764 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