如何Thread.Abort的()工作? [英] How does Thread.Abort() work?

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

问题描述

在无效的输入传递给方法或当对象是即将进入无效状态我们通常抛出异常。让我们看看下面的例子

We usually throw exception when invalid input is passed to a method or when a object is about to enter invalid state. Let's consider the following example

private void SomeMethod(string value)
{
    if(value == null)
        throw new ArgumentNullException("value");
    //Method logic goes here
}

在上面的例子中我插入会抛出 ArgumentNullException throw语句。我的问题是如何运行时管理扔 ThreadAbortException 。显然,这是不可能使用罚球语句中的所有方法,甚至运行时管理扔 ThreadAbortException 在我们的定制方法了。

In the above example I inserted a throw statement which throws ArgumentNullException. My question is how does runtime manages to throw ThreadAbortException. Obviously it is not possible to use a throw statement in all the methods, even runtime manages to throw ThreadAbortException in our custom methods too.

我想知道他们是如何做到的呢?
我很想知道发生了什么在幕后,我开了一个反射器来打开 Thread.Abort的并与该结束了。

I was wondering how do they do it? I was curious to know what is happening behind the scenes, I opened a reflector to open Thread.Abort and end up with this

[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern void AbortInternal();//Implemented in CLR

然后我用Google搜索,发现这个如何ThreadAbortException真正的工作。此链接说,运行时的职位APC通过函数QueueUserAPC 功能,这就是他们做的伎俩。
我不知道函数QueueUserAPC的方法,我只是给一个尝试,看看是否有可能与一些code。继code显示了我试试。

Then I googled and found this How does ThreadAbortException really work. This link says that runtime posts APC through QueueUserAPC function and that's how they do the trick. I wasn't aware of QueueUserAPC method I just gave a try to see whether it is possible with some code. Following code shows my try.

[DllImport("kernel32.dll")]
static extern uint QueueUserAPC(ApcDelegate pfnAPC, IntPtr hThread, UIntPtr dwData);
delegate void ApcDelegate(UIntPtr dwParam);

Thread t = new Thread(Threadproc);
t.Start();
//wait for thread to start
uint result = QueueUserAPC(APC, new IntPtr(nativeId), (UIntPtr)0);//returns zero(fails)
int error = Marshal.GetLastWin32Error();// error also zero

private static void APC(UIntPtr data)
{
    Console.WriteLine("Callback invoked");
}
private static void Threadproc()
{
    //some infinite loop with a sleep
}

如果我做错了什么原谅我,我不知道该怎么做。再次回到问题,有人用这样或CLR团队的一部分知识,可以解释它是如何工作的内部?
如果 APC 是招运行时遵循了我做错了什么?

If am doing something wrong forgive me, I have no idea how to do it. Again back to question, Can somebody with knowledge about this or part of CLR team explain how it works internally? If APC is the trick runtime follows what am doing wrong here?

推荐答案

您务必阅读你指向的页面?最终它归结为:

Are you sure you read the page you were pointing to? In the end it boils down to:

要Thread.Abort的调用归结到.NET的一个线程设置标志被中止,然后在在线程的生命周期的特定点检查标志,如果设置了标志抛出异常。

The call to Thread.Abort boils down to .NET setting a flag on a thread to be aborted and then checking that flag during certain points in the thread’s lifetime, throwing the exception if the flag is set.

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

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