long time=0;
bool b = Task.Factory
.StartNew(() => time = ExecutionTime(LongRunningTask))
.Wait(5000);
if (b == false)
{
MessageBox.Show("Execution took more than 5 seconds.");
}
//time contains the execution time in msec.
Run Code Online (Sandbox Code Playgroud)
public long ExecutionTime(Action action)
{
var sw = Stopwatch.StartNew();
action();
return sw.ElapsedMilliseconds;
}
public void LongRunningTask()
{
Thread.Sleep(10000);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
912 次 |
| 最近记录: |