Call or call multiple methods at once?

how to call several methods while loading my page in asp.net ?? I have 4 methods to call in my page load event. but I want to call all 4 methods and not wait for the first method to finish, and then call the second method.

How to do this in asp.net 4.0?

+7
source share
5 answers
Task[] tasks = new Task[] { new Task(Method0), new Task(Method1), new Task(Method2), new Task(Method3) } foreach(var task in tasks) task.Start(); 

Or shorter

 new Task(Method0).Start(); new Task(Method1).Start(); new Task(Method2).Start(); new Task(Method3).Start(); 
+9
source

First of all, it is important to know whether what you are doing is reasonable. If they are all connected to the CPU, then not , IMO; The web server is already very multithreaded, and, as a rule, this is a busy place to start. The chances are high that you will slow it all down using multiple cores. It will look great for 1 user!

If you are attached to IO, then there are several ways to do this; it would be preferable to use the built-in asynchronous methods that you are talking to, so you can use IOCP rather than regular threads. So for NetworkStream use BeginRead(...) etc.

Then you need to combine everything together. Many ways; I personally tend to use Monitor.Wait and Monitor.Pulse , as this avoids unmanaged code (many wait descriptors are actually provided by the OS).

Also note: threading / parallelism is included with many interesting ways to fail; as a rule, you just need to worry too much about static methods / data for synchronization, but if you have multiple threads in one request doing something: be careful ... there are a lot.

The next version of .NET is designed to make the continuation much easier; I need to see how easily we can apply the current experimental code to IOCP scripts.

+14
source

What you want to do is make asynchronous method calls.

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

+2
source

If you make your methods asynchronous, this will work.

(main stub):

 method1(onReadyCallback1); method2(onReadyCallback2); private void onReadyCallback1() { } 

and etc.

+1
source

Take a look at 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."); } } 
+1
source

All Articles