?
Solved

Need an ASP.Net Multithreading Example

Posted on 2007-11-16
4
Medium Priority
?
2,450 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 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

Technology Partners: 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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

850 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