1. Complete, seconds finished walk next
2. Not completed, 30 s timeout, return
Whether to use multithreading
How to write bosses, please give a thought or write a simple demo
CodePudding user response:
The master thread a open and timingAfter the 30 s, read a thread return value, if not returned, the return to time out
CodePudding user response:
Bool ret=Task. WaitAny (Task, 30000)CodePudding user response:
"Perform an event"? Events usually don't mean that, or this actually has nothing to do with events,Give you write a detection timeout demo:
using System;
Using System. The Threading;
Using System. The Threading. The Tasks;
The namespace ConsoleApp1
{
Class Program
{
The static async Task Main (string [] args)
{
for (var i=0; i <20; I++)
{
Var r=await test ();
Console. WriteLine (return to {$" (r==1? "Timeout" : r.T oString ())} ");
}
}
Private static async TaskThe test ()
{
Var n=new Random (). The Next (500);
Var t1=Task. Delay (300);
Var t2=Task. Run (()=& gt; Thread.sleep (n));//the custom task
//var t2=Task. Run (async ()=& gt; Await Task. Delay (n));
Var t=await Task. WhenAny (t1, t2);
If (t==t1)
return -1;
The else
return n;
}
}
}