Thread.Abort的()方法冻结 [英] Thread.Abort() method freezes

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

问题描述

所以我GOOGLE了它冻结使用不安全code,因为,和AbortException投只有当控制流返回到管理code。所以,在我的情况,我有一个机库,称为一个线程。所以有时我不能放弃,因为图书馆是天然的和放弃的方法不只是什么也不做,但冻结调用线程。

So i've googled that it freezes because of using unsafe code, and AbortException throws only when control flow returns to managed code. So in my case i have a native library, called in a thread. So sometimes i can't abort it, because library is native and Abort method not just do nothing, but freezes calling thread.

所以,我想解决这个问题。

So i'd like to solve it.

例如,采用不同的工艺应该帮助,但它是非常复杂的。

For example, using different process should help, but it's very complicated.

所以不太沉重的解决方案是使用应用程序域。但不管怎样,我创建了一个exe和调用它。我试图生成在内存中像这样

So less heavy solution is use AppDomains. But anyway i should create an exe and call it. I tried to generate it in memory like this

var appDomain = AppDomain.CreateDomain("newDomain");
var assemblyBuilder = appDomain.DefineDynamicAssembly(new AssemblyName("myAsm"), AssemblyBuilderAccess.RunAndCollect);
var module = assemblyBuilder.DefineDynamicModule("myDynamicModule");
var type = module.DefineType("myStaticBulder", TypeAttributes.Public);
var methBuilder = type.DefineMethod("exec", MethodAttributes.Static | MethodAttributes.Public);
var ilGenerator = methBuilder.GetILGenerator();

但我发现只有EMIT路,这是非常非常复杂的。

but i found only EMIT-way, it's very very complicated.

请问肤浅的解决方案存在?

Does superficial solution exists?

推荐答案

这不能由设计工作。在CLR拥有的非常的严格什么样的code可以安全地中止规则。这一点很重要,超出了不明智的使用Thread.Abort的(),很多情况下CLR的必须的中止code,应用程序域卸载是最重要的。

This cannot work by design. The CLR has very strict rules about what kind of code can safely be aborted. It is important, beyond the unwise use of Thread.Abort(), plenty of cases where the CLR must abort code, AppDomain unloads being foremost.

铁一般的规则是,在CLR必须相信这是的安全的中止code。它只是确信,如果线程忙于执行管理code或正在等待一个管理的同步对象。你的情况做的不可以资格,没办法为CLR有任何想法,土著code在做什么。中止在这种状态下一个线程几乎从来不导致问题。的Thread.Abort的()的危险相同的想法,但乘以一千。在内部操作系统锁定随后的僵局很可能,完全undebuggable。

The iron-clad rule is that the CLR must be convinced that it is safe to abort the code. It is only convinced of that if the thread is busy executing managed code or is waiting on a managed synchronization object. Your case does not qualify, no way for the CLR to have any idea what that native code is doing. Aborting a thread in such a state almost never not causes problems. Same idea of the danger of Thread.Abort() but multiplied by a thousand. A subsequent deadlock on an internal operating system lock is very likely, utterly undebuggable.

一个AppDomain因此的没有的一个解决方案,或者说,它不能被卸载,直到线程停止运行,它不会。

An AppDomain therefore is not a solution either, it cannot be unloaded until the thread stopped running and it won't.

你唯一可以做的是隔离code在一个单独的进程。写,通过一个标准的.NET IPC机制就像一个套接字,命名管道,内存映射文件,远程处理或WCF暴露了其API一个小帮手EXE项目。当code挂起,你可以安全地Process.Kill()它。无破损都可以做,整个过程状态扔掉。恢复往往不过是相当棘手的,你还必须得到重新启动过程,并把它放回原来的状态。尤其是国家恢复通常是很难做到可靠。

Only thing you can do is isolate that code in a separate process. Write a little helper EXE project that exposes its api through a standard .NET IPC mechanism like a socket, named pipe, memory mapped file, remoting or WCF. When the code hangs, you can safely Process.Kill() it. No damage can be done, the entire process state is thrown away. Recovering tends to be quite tricky however, you still do have to get the process restarted and get it back into the original state. Especially the state restoration is usually very difficult to do reliably.

这篇关于Thread.Abort的()方法冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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