Previous post:. NET: How do I get thread support to time out? It has been stated that the current multithreaded scenario for Microsoft Main push is task: Note: task best refers to. NET4.5. 4.0 is OK, but not mature. Thread Reference 2.0 is enough.
1. The task created by the constructor must start manually, and the task created by the factory is started directly.
var task1 = new Task (() = {}); Task. Start ();
var task2 = Task.Factory.StartNew (() =>{});
task[] tasks = new TASK[2];
var tasks[0] = Task.Factory.StartNew (() =>{});
var tasks[1] = Task.Factory.StartNew (() = {});
2. Task1. Status:created;waitingtorun; rantocompletion;
3. Task1. Wait (); Task.waitall (Task1, Task2); Task.waitany (Task1, task2);
4. var result = Task1. Continuewith<string> (task = {}); <> returns a value type for a task.
5. Cancellation:
var tokensource = new CancellationTokenSource ();
var token = Tokensource.token;
var task = Task.Factory.StartNew (() =>{if (token. iscancellationrequested) {return;}//execute the content. }, token);
Token. Register (() = {Console.WriteLine ("Canceled");});
Tokensource.cancel ();
Example 2:cancellationtokensource CTS =new CancellationTokenSource ();
Task<int> t=new task<int> (() =>add (CTS. Token), CTS. Token); ADD (CancellationToken CT) for the task performed
T.start ();
T.continuewith (taskended);//taskended tasks to perform when completed
Third, the Mission factory TaskFactory: and Task.factory similar. But TaskFactory can share the task state, such as sharing the CancellationTokenSource, you can cancel a group of tasks at the same time.
CancellationTokenSource cts = new CancellationTokenSource ();
TaskFactory TaskFactory = new TaskFactory ();
task[] tasks = new task[]
{
Taskfactory.startnew (() = ADD (cts. Token)),
Taskfactory.startnew (() = ADD (cts. Token)),
Taskfactory.startnew (() = ADD (cts. Token))
};
//cancellationtoken.none indicates that tasksended cannot be canceled
Taskfactory.continuewhenall (Tasks, tasksended, cancellationtoken.none);
Console.readkey ();
Cts. Cancel ();
Console.readkey ();
Tasksended (task[] tasks) is the content that is executed when canceled.
The Continuewhenall and Continuewhenany methods are performed after a set of tasks are completed.
Reference documents:
http://blog.csdn.net/djc11282/article/details/17524013
Http://www.cnblogs.com/luminji/archive/2011/05/13/2044801.html
Second, the use of AutoResetEvent to wait for the user input after execution:
private void Button1_Click (object sender, EventArgs e)
{
Thread th = new Thread (yourthread);
Th. Start ();
}
private void textBox1_TextChanged (object sender, EventArgs e)
{
if (textBox1.Text.Length >= 4)
{
Detailcollectedevent.set (); When TextBox1 text exceeds 4 digits, send a notification
}
}
AutoResetEvent detailcollectedevent = new AutoResetEvent (false);
void Yourthread ()
{
MessageBox.Show ("input bank account details into the textbox");
Detailcollectedevent.waitone (); Waiting for notification
MessageBox.Show ("We'll keep the Secret.");
}
Originally wanted to write a hang Q tool, so only to study the above content, because hang Q must adopt multi-threading Factory mode TaskFactory. and need to enter a verification code, it is necessary to wait for the user input AutoResetEvent, but later found that there is a relatively perfect hanging Q tool, so did not continue to write, http://www.qyisoft.com/webqq hang Q tool
This paper is written by the normal distribution x~n (μ,σ2) QQ2052702900 in 8 months of the year .
"Note" All texts are summaries of individual learning, only if the individual is retained. Jo Jun unfortunate Touring, do not ridicule, there is a merit, may wish to borrow, do not have errors and omissions, but also look at the liberal enlighten.
Multithreaded--task, waiting for user input AutoResetEvent