问题
I have windows project and one form which have timer for each 5 seconds.
It calls and processes methods from request named table time wise and condition wise.
But I have some methods types which takes too much time to respond and want those methods in separate thread. So that I can run those both request types in separate threads and syncs.
How can I do separate those both using thread -- multi async threads?
回答1:
I recommend you look at the .NET 4.0 Task
class. Firing full threads every time might be overkill. Tasks, together with timers use the underlying thread pool to execute work in parallel.
Using a Task
is as simple as:
Task t = Task.Factory.StartNew(
() =>
{
// task code here
});
回答2:
using System;
using System.Threading;
class Program
{
static void Main()
{
Thread thread1 = new Thread(new ThreadStart(A));
Thread thread2 = new Thread(new ThreadStart(B));
thread1.Start();
thread2.Start();
thread1.Join();
thread2.Join();
}
static void A()
{
Thread.Sleep(100);
Console.WriteLine('A');
}
static void B()
{
Thread.Sleep(1000);
Console.WriteLine('B');
}
}
Threading Tutorial
来源:https://stackoverflow.com/questions/11985308/multiple-threads-in-windows-service