C# 4.5以下超时

//超时方法
public object TimeOut(Func<string> action, int time)
{
var iar = action.BeginInvoke(null, action);
//
if (!iar.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(time))) //下载文件超过150S就 超时处理
{
return string.Empty;
}
return action.EndInvoke(iar);
}

//使用

Func<string> action1 = () => DownLoadFile(50);
string str = TimeOut(action1,5).ToString();

原文地址:https://www.cnblogs.com/qizh/p/5332890.html