public static void InvokeTimeOut(Action method, int milliseconds)
{
Thread thdToKill = null;
Action invokemethod = new Action(() =>
{
thdToKill = Thread.CurrentThread;
method();
});
IAsyncResult ar = invokemethod.BeginInvoke(null, null);
if (!ar.AsyncWaitHandle.WaitOne(milliseconds))
{
thdToKill.Abort();
throw new Exception(string.Format("操作失败,原因:超时 {0}毫秒", milliseconds));
}
invokemethod.EndInvoke(ar);
}