是Thread.Abort的()腐败本地化? [英] Is Thread.Abort() corruption localized?

查看:144
本文介绍了是Thread.Abort的()腐败本地化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,调用Thread.Abort的()是由于各种原因坏的和错误

I understand that calling Thread.Abort() is BAD AND WRONG because of various reasons

  1. 可锁定或其他重要区域
  2. 可能是通过中间的I / O操作,资源将无法得到清理
  3. code可以吞下ThreadAbortException
  4. 可能是通过中间的状态变化的操作,并退出该计划处于无效状态。

假设该线程的run()方法:

Assuming that the thread's Run() method:

  1. 在不使用并发原语。
  2. 在不执行任何I / O。
  3. 在不下咽这种例外。
  4. 暴露于只读API,因此不能改变程序的状态。

问:

如果我要调用abort()这样的线程,完成(与问候腐败)的伤害本地化?正如,我可以靠我的程序,SANS最近中止线程,继续正常的行为,或者是我的整个进程/ AppDomain中/?那么潜在的损坏?

If I were to call Abort() on such a thread, is the damage done (with regards to corruption) localized? As in, can I rely on my program, sans the recently aborted thread, to continue to behave normally, or is my entire process/AppDomain/?? then potentially corrupted?

如果我删除了假设1和2将中止()腐败被定位于特定的并发原语和I / O资源是访问?或者,可以在propogate向外?

If I remove assumptions 1 and 2 would abort() corruption be localized to the specific concurrency primitives and i/o resources it accessed? Or can it propogate outwards?

为什么我鞭笞Thread.Abort的()死马

我正在写一个胖客户端的C#WinForms应用程序。一个特征S为用户编写code在C#中,它实现的接口上,然后可以插入我的应用我的域对象(code编译成通过codeDOM的组装和使用反射加载的能力);

I'm writing a thick client c# winforms application. One feature s the ability for the user to write code in c# which implements interfaces on my domain objects which can then be plugged into my application (code is compiled into an assembly via codedom and loaded using reflection);

这样的想法,那么用户可以写

So the idea is the user could then write

public class CustomComputation : IComputationThing
{
    public void Compute(object context)
    {
        while (true) ;
    }
}

然后我可以反映生成的程序集,提取物和实例 CustomComputation 的实例,并调用计算(...); pretty的简单的东西。

and I could then reflect on the generated assembly, extract and instantiate an instance of CustomComputation, and call Compute(...); Pretty straightforward stuff.

现在的问题是,用户可以接着写,好了,上面 - 一个无限循环,或其他一些指数时间计算,他决定,他不希望等待

The problem is that the user could then write, well, the above - an infinite loop, or some other exponential time computation he decides he doesn't want to wait for.

我想调查我的选择提供了最大努力恢复,当用户写一些无限循环(或指数时间等)code,并希望从中止。虽然我不打算要求用户坚持取消模式,但最终没有保证,他们的意志。

I am trying to investigate my options for providing a best-effort recovery for when users write some infinite-looping (or exponential time etc) code and wants to abort from that. While I do intend to ask users to adhere to a cancellation pattern but ultimately there is no guarantee that they will.

推荐答案

正如刚才Vilx-你可以考虑把用户code在另一个AppDomain中。

As mentioned by Vilx- you could consider putting the "user" code in another AppDomain.

事实上,由于净3.5,.NET框架包括了 System.AddIn 命名空间,它提供了隔离附加$ C的一种简化的方式在单独的应用程序域$ C(除其他事项外)。只要是不需要太多的跨域通信,这给你隔离一个不错的水平,从而在最糟糕的情况是,你拆掉一个AppDomain特定于用户code。

In fact, since .Net 3.5, the .Net Framework has included the System.AddIn namespace, which provides a simplified way of isolating "add-in" code in seperate app domains (amongst other things). Provided there isn't too much cross-domain communication required, this gives you a nice level of isolation whereby the worst case scenario is that you tear down an AppDomain that is specific to the user code.

请参阅此 MSDN 文章和的 CLR的加载项团队的博客了解详情

See this MSDN article and the CLR-Addin team's blog for more information.

此外,虽然它并没有解决所有问题,你可以通过烧制了Thread.interrupt开始。这将导致一个ThreadInterruptException待提高以类似的方式来ThreadAbortException,所不同的是将发生它oinly在在code中定义点;当线程块或睡觉,而不是绝对的任何地方。它可能不会在你上面提到的紧环例如帮助,但会如果循环有一个的Thread.Sleep(0)。

Additionally, although it doesn't solve everything, you could start by firing a Thread.Interrupt. This will cause a ThreadInterruptException to be raised in a similar way to ThreadAbortException, except that it will occur oinly at defined points in the code; when the thread blocks or sleeps, rather than absolutely anywhere. It probably won't help in the tight-loop example you mention above, but would if the loop had a Thread.Sleep(0).

您可以随时毕业你的选择:

You can always graduate your options:

  • 在画上了句号()方法IComputationThing,并要求用户来实现它。
  • 如果某个加载项没有响应停止()及时,尝试了Thread.interrupt(和文档紧尺蠖可能要放的Thread.Sleep(0)在它们的环屈服于另一个线程)
  • 如果失败,请尝试Thread.Abort的,或推倒应用程序域(可能是直接后者)。

这篇关于是Thread.Abort的()腐败本地化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆