zoukankan      html  css  js  c++  java
  • 方法执行超时则终止方法思路

     

    今天想一个方法的超时则强行终止该方法思路,处于实验阶段,尚未正式使用。总体思路如下:

    1,把需要执行的方法放在一个线程中A,执行。

    2,把线程A作为参数传入线程B中,在B中执行一个循环,如果超时则强行终止该线程。

    代码如下:

    详细代码
    public interface IExcecMehtod
    {
         
    void ExecMethodWithLongTime();
    }
    public class MethodExecHelper
    {
         
    #region 全局变量
         IExcecMehtod methodHost 
    = null;
         
    public IExcecMehtod MethodHost
         {
             
    get { return methodHost; }
             
    set { methodHost = value; }
         }
         
    private int timeOut;
         
    /// <summary>
         
    /// 方法执行超时时间(单位:秒)
         
    /// </summary>
         public int TimeOut
         {
             
    get
             {
                 
    return timeOut;
             }
             
    set
             {
                 timeOut 
    = value;
             }
         }
         
    #endregion
         
    #region 构造函数
         
    public MethodExecHelper(IExcecMehtod methodHost, int timeOut)
         {
             
    this.methodHost = methodHost;
             
    this.timeOut = timeOut;
         }
         
    public MethodExecHelper(IExcecMehtod methodHost)
             : 
    this(methodHost, 5)
         {
         }
         
    #endregion
         
    #region 对外方法
         
    public void Run()
         {
             Thread execThread 
    = new Thread(new ThreadStart(WaitMethod));
             execThread.Start();
             Thread abortThread 
    = new Thread(new ParameterizedThreadStart(AbortMehtod));
             abortThread.Start(execThread);
         }
         
    #endregion
         
    #region 私有方法
         
    void AbortMehtod(object state)
         {
             
    bool flag = false;
             DateTime beginTime 
    = DateTime.Now;
             
    while (!flag)
             {
                 DateTime currentTime 
    = DateTime.Now;
                 
    if ((currentTime - beginTime).Seconds > TimeOut)
                 {
                     Thread thread 
    = state as Thread;
                     
    if (thread != null)
                     {
                         
    if (thread.IsAlive)
                         {
                             thread.Abort();
                             Debug.WriteLine(
    "线程强行终止");
                         }
                     }
                     flag 
    = true;
                 }
                 Thread.Sleep(
    1000);
             }
         }
         
    void WaitMethod()
         {
             MethodHost.ExecMethodWithLongTime();
             Debug.WriteLine(
    "线程执行完成");
         }
         
    #endregion
    }
  • 相关阅读:
    Yii中缓存依赖的处理
    简单实现Tab切换(带框架)
    Server Application Unavailable出现的原因及解决方案集锦
    Microsoft.ReportViewer.WebForms, Version=10.0.0.0的报错问题,解决方案
    ASP.NET中树型DropDownList的绑定
    VS2010 使用时选择代码或双击时出错,点击窗口按钮后VS自动重启问题
    js实现文本框限制输入数字和小数点--兼容多个浏览器
    输入的全角字符转换成半角字符--css、js、ASP.NET
    sql把表格拼成字符串,多半使用于GROUP BY
    Sql将逗号分隔的字符串分拆成表格的方法
  • 原文地址:https://www.cnblogs.com/wangn/p/1615078.html
Copyright © 2011-2022 走看看