加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何为各种代码块创建通用超时对象?

发布时间:2020-12-15 07:59:59 所属栏目:百科 来源:网络整理
导读:我有一系列代码块花了太长时间.它失败时我不需要任何技巧.事实上,我想在这些块花费太长时间时抛出异常,并且只是通过我们的标准错误处理而失败.我宁愿不为每个块创建方法(这是我到目前为止看到的唯一建议),因为它需要重写代码库. 如果可能的话,这就是我想要创
我有一系列代码块花了太长时间.它失败时我不需要任何技巧.事实上,我想在这些块花费太长时间时抛出异常,并且只是通过我们的标准错误处理而失败.我宁愿不为每个块创建方法(这是我到目前为止看到的唯一建议),因为它需要重写代码库.

如果可能的话,这就是我想要创建的内容.

public void MyMethod( ... )
{

 ...

    using (MyTimeoutObject mto = new MyTimeoutObject(new TimeSpan(0,30)))
    {
        // Everything in here must complete within the timespan
        // or mto will throw an exception. When the using block
        // disposes of mto,then the timer is disabled and 
        // disaster is averted.
    }

 ...
}

我使用Timer类创建了一个简单的对象. (注意那些喜欢复制/粘贴的人:这个代码不起作用!!)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

    public class MyTimeoutObject : IDisposable
    {
        private Timer timer = null;

        public MyTimeoutObject (TimeSpan ts)
        {
            timer = new Timer();
            timer.Elapsed += timer_Elapsed;
            timer.Interval = ts.TotalMilliseconds;

            timer.Start();
        }

        void timer_Elapsed(object sender,ElapsedEventArgs e)
        {
            throw new TimeoutException("A code block has timed out.");
        }

        public void Dispose()
        {
            if (timer != null)
            {
                timer.Stop();
            }
        }
    }

它不起作用,因为System.Timers.Timer类捕获,吸收和忽略在其中抛出的任何异常,正如我所发现的那样 – 击败了我的设计.在没有完全重新设计的情况下创建此类/功能的任何其他方式?

两个小时前这似乎很简单,但让我很头疼.

解决方法

好的,我花了一些时间在这个上面,我想我有一个适合你的解决方案,而不必更改你的代码.

以下是如何使用我创建的Timebox类.

public void MyMethod( ... ) {

    // some stuff

    // instead of this
    // using(...){ /* your code here */ }

    // you can use this
    var timebox = new Timebox(TimeSpan.FromSeconds(1));
    timebox.Execute(() =>
    {
        /* your code here */
    });

    // some more stuff

}

以下是Timebox的工作原理.

>使用给定的Timespan创建Timebox对象
>调用Execute时,Timebox会创建一个子AppDomain来保存TimeboxRuntime对象引用,并向其返回一个代理
>子AppDomain中的TimeboxRuntime对象将Action作为输入在子域中执行
> Timebox然后创建一个任务来调用TimeboxRuntime代理
>任务已启动(并且操作执行开始),并且“main”线程等待给定的TimeSpan
>在给定的TimeSpan之后(或任务完成时),无论Action是否完成,都会卸载子AppDomain.
>如果操作超时,则抛出TimeoutException,否则如果操作抛出异常,则由子AppDomain捕获并返回以调用AppDomain

缺点是您的程序需要足够高的权限才能创建AppDomain.

这是一个示例程序,演示它是如何工作的(我相信如果包含正确的用法,你可以复制粘贴它).如果你有兴趣,我也创建了this gist.

public class Program
{
    public static void Main()
    {
        try
        {
            var timebox = new Timebox(TimeSpan.FromSeconds(1));
            timebox.Execute(() =>
            {
                // do your thing
                for (var i = 0; i < 1000; i++)
                {
                    Console.WriteLine(i);
                }
            });

            Console.WriteLine("Didn't Time Out");
        }
        catch (TimeoutException e)
        {
            Console.WriteLine("Timed Out");
            // handle it
        }
        catch(Exception e)
        {
            Console.WriteLine("Another exception was thrown in your timeboxed function");
            // handle it
        }
        Console.WriteLine("Program Finished");
        Console.ReadLine();
    }
}

public class Timebox
{
    private readonly TimeSpan _ts;

    public Timebox(TimeSpan ts)
    {
        _ts = ts;
    }

    public void Execute(Action func)
    {
        AppDomain childDomain = null;
        try
        {
            // Construct and initialize settings for a second AppDomain.  Perhaps some of
            // this is unnecessary but perhaps not.
            var domainSetup = new AppDomainSetup()
            {
                ApplicationBase = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,ApplicationName = AppDomain.CurrentDomain.SetupInformation.ApplicationName,LoaderOptimization = LoaderOptimization.MultiDomainHost
            };

            // Create the child AppDomain
            childDomain = AppDomain.CreateDomain("Timebox Domain",null,domainSetup);

            // Create an instance of the timebox runtime child AppDomain
            var timeboxRuntime = (ITimeboxRuntime)childDomain.CreateInstanceAndUnwrap(
                typeof(TimeboxRuntime).Assembly.FullName,typeof(TimeboxRuntime).FullName);

            // Start the runtime,by passing it the function we're timboxing
            Exception ex = null;
            var timeoutOccurred = true;
            var task = new Task(() =>
            {
                ex = timeboxRuntime.Run(func);
                timeoutOccurred = false;
            });

            // start task,and wait for the alloted timespan.  If the method doesn't finish
            // by then,then we kill the childDomain and throw a TimeoutException
            task.Start();
            task.Wait(_ts);

            // if the timeout occurred then we throw the exception for the caller to handle.
            if(timeoutOccurred)
            {
                throw new TimeoutException("The child domain timed out");
            }

            // If no timeout occurred,then throw whatever exception was thrown
            // by our child AppDomain,so that calling code "sees" the exception
            // thrown by the code that it passes in.
            if(ex != null)
            {
                throw ex;
            }
        }
        finally
        {
            // kill the child domain whether or not the function has completed
            if(childDomain != null) AppDomain.Unload(childDomain);
        }
    }

    // don't strictly need this,but I prefer having an interface point to the proxy
    private interface ITimeboxRuntime
    {
        Exception Run(Action action);
    }

    // Need to derive from MarshalByRefObject... proxy is returned across AppDomain boundary.
    private class TimeboxRuntime : MarshalByRefObject,ITimeboxRuntime
    {
        public Exception Run(Action action)
        {
            try
            {
                // Nike: just do it!
                action();
            }
            catch(Exception e)
            {
                // return the exception to be thrown in the calling AppDomain
                return e;
            }
            return null;
        }
    }
}

编辑:

之所以我使用AppDomain而不是仅使用线程或任务,是因为没有用于终止任意代码的线程或任务的防弹方式[2] [3].根据您的要求,AppDomain对我来说似乎是最好的方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读