开发者

Invoke or call more than one method at same time?

how 开发者_JS百科to call more than one method at same time in my page load in asp.net?? i have 4 methods to called in my page load event. but i want to call all 4 methods and not wait for 1st method to get finish then call 2nd method.

How to achieve this is in asp.net 4.0?


First off, it is important to know if what you are doing is sensible. If they are all CPU-bound, then do not do this, IMO; a web server is already highly threaded, and is usually a busy place to begin with. Chances are high that you will slow the whole thing down by using multiple cores. It'll look great for 1 user, though!

If you are IO-bound, then there are any number of ways to do this; the preferred would be to use the inbuilt async methods of whatever you are talking to, so you can use IOCP rather than regular threads. So for a NetworkStream, you would use BeginRead(...) etc.

Then you need to join everything together. Lots more ways; personally I tend to use Monitor.Wait and Monitor.Pulse, as this avoids going to unmanaged code (many wait-handles are actually OS-provided).

Also note: threading/parallelism comes in a bundle alongside many fun ways to fail; normally you only need to worry overly about static methods/data for synchronization, but if you have multiple threads in a single request doing things: watch out for bumps... there are many.

The next version of .NET is meant to make continuations a lot easier; I need to take a look to see how easily we can apply the current experimental code to IOCP scenarios.


Task[] tasks = new Task[]
{
   new Task(Method0),
   new Task(Method1),
   new Task(Method2),
   new Task(Method3)
}

foreach(var task in tasks)
   task.Start();

Or more shortly

new Task(Method0).Start();
new Task(Method1).Start();
new Task(Method2).Start();
new Task(Method3).Start();


What you want to do is make asynchronous method calls.

http://www.codeproject.com/KB/cs/AsyncMethodInvocation.aspx


If you make your methods asynchronous it would work.

(basic stub):

method1(onReadyCallback1);
method2(onReadyCallback2);

private void onReadyCallback1() {

}

etc.


Take a look at the ThreadPool.QueueUserWorkItem and then here for an example

from the MSDN documentation

using System;
using System.Threading;
public class Example {
    public static void Main() {
        // Queue the task.
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));

        Console.WriteLine("Main thread does some work, then sleeps.");
        // If you comment out the Sleep, the main thread exits before
        // the thread pool task runs.  The thread pool uses background
        // threads, which do not keep the application running.  (This
        // is a simple example of a race condition.)
        Thread.Sleep(1000);

        Console.WriteLine("Main thread exits.");
    }

    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo) {
        // No state object was passed to QueueUserWorkItem, so 
        // stateInfo is null.
        Console.WriteLine("Hello from the thread pool.");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜