Solved

Need an ASP.Net Multithreading Example

Posted on 2007-11-16
4
2,443 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 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

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

729 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