Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Threading;
Namespace multithreaded Task
{
Class Program
{
static void Main (string[] args)
{
Task task = new Task (
() =
{
for (int i = 0; i <; i++)
{
Console.WriteLine ("Index:{0},threadid:{1}", I,thread.currentthread.managedthreadid);
Thread.Sleep (1000);
}
});
It is possible to block threads when using the wait method of the task class to wait for a task or the result property of a derived class to get the results of a task execution.
To solve this problem, you can use the ContinueWith method, which automatically launches a new task to handle the execution results when a task is completed.
Task. ContinueWith (t =
{
Console.WriteLine ("Executed!,threadid:{0}", Thread.CurrentThread.ManagedThreadId);
});
Task. Start ();
Console.WriteLine ("The main thread is finished! Threadid:{0} ", Thread.CurrentThread.ManagedThreadId);
Console.readkey ();
}
}
}
C # multithreaded Task