C# 给某个方法设定执行超时时间

来源:互联网 发布:php抓取网页匹配url 编辑:程序博客网 时间:2024/04/29 15:07

http://www.cnblogs.com/08shiyan/archive/2011/07/30/2122183.html

C# 给某个方法设定执行超时时间

在某些情况下(例如通过网络访问数据),常常不希望程序卡住而占用太多时间以至于造成界面假死。

在这时、我们可以通过Thread、Thread + Invoke(UI)或者是 delegate.BeginInvoke 来避免界面假死,

但是这样做时,某些代码或者是某个方法的执行超时的时间还是无法操控的。
那么我们又是否有一种比较通用的方法、来设定某一个方法的执行超时的时间,让其一旦超过指定时间则跳出指定方法、进而继续向下执行呢?

答案当然是肯定的。

 

delegate.BeginInvoke可以实现代码代码的异步执行,在这种情况下,只要让程序可以等待一个Timespan,如果在Timespan到达之前方法内的代码还没有执行完毕、说明该方法执行超时了。

那么关键的就是“等待一个Timespan”,而恰好.NET 里提供了一些类和方法来实现该功能。我这里选用的是ManualResetEvent.WaitOne(timespan, false);其返回值代码其是否在特定时间内收到信号,而我们恰好可以利用这个布尔值 外加一个标记变量 来判断一个方法是否执行超时。

 

 

相关的实现代码如下:

   

复制代码
publicdelegatevoid DoHandler();
publicclass Timeout
{
private ManualResetEvent mTimeoutObject;
//标记变量
privatebool mBoTimeout;
public DoHandler Do;

public Timeout()
{
// 初始状态为 停止
this.mTimeoutObject =new ManualResetEvent(true);
}
///<summary>
/// 指定超时时间 异步执行某个方法
///</summary>
///<returns>执行 是否超时</returns>
publicbool DoWithTimeout(TimeSpan timeSpan)
{
if (this.Do ==null)
{
returnfalse;
}
this.mTimeoutObject.Reset();
this.mBoTimeout =true; //标记
this.Do.BeginInvoke(DoAsyncCallBack, null);
// 等待 信号Set
if (!this.mTimeoutObject.WaitOne(timeSpan, false))
{
this.mBoTimeout =true;
}
returnthis.mBoTimeout;
}
///<summary>
/// 异步委托 回调函数
///</summary>
///<param name="result"></param>
privatevoid DoAsyncCallBack(IAsyncResult result)
{
try
{
this.Do.EndInvoke(result);
// 指示方法的执行未超时
this.mBoTimeout =false;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
this.mBoTimeout =true;
}
finally
{
this.mTimeoutObject.Set();
}
}
}
复制代码

 

测试代码如下:

   

复制代码
class Program
{
privatestatic Stopwatch watch;
privatestatic System.Threading.Timer timer;

[STAThread]
staticvoid Main(string[] args)
{
watch
=new Stopwatch();
Timeout timeout
=new Timeout();
timeout.Do
=new Program().DoSomething;
watch.Start();
timer
=new System.Threading.Timer(timerCallBack, null, 0, 500);
Console.WriteLine(
"4秒超时开始执行");
bool bo = timeout.DoWithTimeout(new TimeSpan(0, 0, 0, 4));
Console.WriteLine(
string.Format("4秒超时执行结果,是否超时:{0}", bo));
Console.WriteLine(
"***************************************************");

timeout
=new Timeout();
timeout.Do
=new Program().DoSomething;
Console.WriteLine(
"6秒超时开始执行");
bo
= timeout.DoWithTimeout(new TimeSpan(0, 0, 0, 6));
Console.WriteLine(
string.Format("6秒超时执行结果,是否超时:{0}", bo));

timerCallBack(
null);

watch.Stop();
timer.Dispose();
Console.ReadLine();
}
staticvoid timerCallBack(object obj)
{
Console.WriteLine(
string.Format("运行时间:{0}秒", watch.Elapsed.TotalSeconds.ToString("F2")));
}
publicvoid DoSomething()
{
// 休眠 5秒
System.Threading.Thread.Sleep(new TimeSpan(0, 0, 0, 5));
}
}

复制代码

测试代码执行结果如下:
 

由上可得知:设定超时时间为4秒执行方法 DoSomething,执行结果为超时,并且在4秒后跳出方法DoSomething继续向下执行。



C# 超时工具类 第二版

附源码,没有附测试demo

 

之前的工具类:C# 给某个方法设定执行超时时间

 

复制代码
    /// <summary>    /// 超时工具    /// </summary>    public class TimeoutTools : IDisposable    {        private System.Windows.Forms.Timer timer;        /// <summary>        /// 位置        /// </summary>        public uint Position { get; private set; }        /// <summary>        /// 超时事件        /// </summary>        public event EventHandler TimeoutEvent;        /// <summary>        /// Tick事件        /// </summary>        public event EventHandler TickEvent;        /// <summary>        /// 步长值        /// <para>默认值1</para>        /// </summary>        public uint StepLength { get; set; }        /// <summary>        /// 超时长度        /// <para>默认180</para>        /// </summary>        public uint TimeoutLength { get; set; }        /// <summary>        /// 默认构造函数        /// </summary>        public TimeoutTools(System.ComponentModel.IContainer container)        {            this.StepLength = 1;            this.TimeoutLength = 180;            this.timer = new System.Windows.Forms.Timer(container);            this.timer.Interval = 1000;            this.timer.Enabled = false;            timer.Tick += (sender, e) =>            {                this.Position += this.StepLength;                if (this.Position >= this.TimeoutLength)                {                    this.Reset();                    this.OnTimeOut();                }                else                {                    if (this.TickEvent != null)                    {                        this.TickEvent(this, EventArgs.Empty);                    }                }            };        }        ///<summary>                /// 指定超时时间 执行某个方法                ///</summary>        ///<returns>执行 是否超时</returns>        public static bool DoAction(TimeSpan timeSpan, Action action)        {            if (action == null)                throw new ArgumentNullException("action is null");            bool timeout = true;            try            {                // 异步调用Action                IAsyncResult result = action.BeginInvoke(null, null);                // Wait                if (result.AsyncWaitHandle.WaitOne(timeSpan, false))                {                    timeout = false;                }                if (!timeout)                {                    action.EndInvoke(result);                }            }            catch (Exception)            {                timeout = true;            }            return timeout;        }        /// <summary>        /// 设置计时器参数        /// </summary>        /// <param name="period">毫秒</param>        public void SetInterval(int period)        {            if (period == Timeout.Infinite)            {                this.timer.Enabled = false;                return;            }            this.timer.Interval = period;            this.timer.Enabled = true;        }        /// <summary>        /// 接收到信号        /// </summary>        public void Reset()        {            this.Position = 0;        }        protected void OnTimeOut()        {            if (this.TimeoutEvent != null)            {                this.TimeoutEvent(this, EventArgs.Empty);            }        }        public void Dispose()        {            if (this.timer == null)                return;            this.timer.Enabled = false;            this.timer.Dispose();        }    }
复制代码

原创粉丝点击