懒人记时 代码仓库
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

64 lines
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;
}
}
}
}