懒人记时 代码仓库
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

64 行
1.3 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AppTime
{
static class Utils
{
public static T CheckTimeout<T>(Func<T> act, Func<Thread, T> whenTimeout, int timeoutMs, bool abortThread = true)
{
var isTimeout = true;
Exception ex = null;
var t = new Thread(() =>
{
try
{
act();
isTimeout = false;
}
catch (Exception e)
{
ex = e;
}
});
t.Start();
t.Join(timeoutMs);
if (isTimeout)
{
if (abortThread)
{
t.Abort();
}
return whenTimeout(t);
}
if (ex != null)
{
throw ex;
}
return default(T);
}
public static Exception Try(Action act)
{
try
{
act();
return null;
}
catch (Exception ex)
{
return ex;
}
}
}
}