Thread.Abort的() [英] thread.Abort()

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

问题描述

从此页面:
http:// www .c-sharpcorner.com / 2 / mt_beginner1.asp

调用线程类的Abort方法永久终止线程。制作

确保在中止前拨打IsAlive。


if(thread.IsAlive)

{

thread.Abort();

}


我的问题是,如果线程完成它正在做的事情会发生什么

在检查IsAlive和调用Abort()之间是什么?


线程并不是我见过这样的事情的唯一地方,而且我总是

只是假设他们已经考虑过了(而且无论如何都不可能),但我知道我们不会写申请说不,它会永远不会发生现在

我很好奇!


-

黛西奶牛

解决方案



" Daisy" <哒*** @ nospam.oops>在消息中写道

news:c0 ********** @ linux01.dannytuppeny.com ...

从这个页面:
< a rel =nofollowhref =http://www.c-sharpcorner.com/2/mt_beginner1.asptarget =_ blank> http://www.c-sharpcorner.com/2/mt_beginner1.asp

调用线程类的Abort方法永久终止线程。确保你在Abort之前给IsAlive打电话。

if(thread.IsAlive)
{
thread.Abort();
}

我的问题是,如果线程在IsAlive被检查和Abort()被调用之间完成了它正在做什么会发生什么?


我不喜欢知道作者在做出这样的陈述时所想的是什么,但是对于怀疑它是正确的(IMO)。代码片段没有任何东西可以检测或阻止竞争条件,我不知道为什么他觉得无论如何都需要测试这个条件。无论如何,

没有使用Thread.Abort有很多原因。
线程并不是我见过这样的事情的唯一地方,我已经总是
只是假设他们已经考虑过了(而且无论如何都不可能),但我知道我们不会写申请说不,它永远不会发生。和
现在我很好奇!



你是对的;有许多代码片段忽略了竞争条件。

编写正确的多线程代码是一项非常困难的任务,甚至经过彻底测试的生产代码也可能存在潜伏在其中的漏洞。

还有很多人认为他们知道这些问题,但实际上并非如此。


Dave

根据我的经验,在已经终止

的线程上调用Abort()并不会导致抛出异常。以下运行以下

代码示例。调用IsAlive()是一个很好的做法。


使用System;

使用System.Threading;


名称空间ConsoleTester

{

///< summary>

/// Class1的摘要说明。

/ //< / summary>


class测试

{

public static void Main()

{

线程newThread =新线程(新的ThreadStart(TestMethod));

newThread.Start();

Thread.Sleep( 1000);


//中止newThread。

Console.WriteLine(主要中止新线程。);

newThread.Abort(" Main from Main。);


//等待线程终止。

newThread.Join();

Console.WriteLine(新线程终止 - 主要退出。);

Console.ReadLine();

}


static void TestMethod()

{

试试

{

// while(true)

{

Console.WriteLine(新线程运行。 );

//Thread.Sleep(1000);

}

Console.WriteLine(" New thread exiting ..." );

}

catch(ThreadAbortException abortException)

{

Console.WriteLine((string)abortException。 ExceptionState);

}

}

}


}

希望这会有所帮助。

-

Jeffrey Wynn
jw***@nospam.net

雏菊 <哒*** @ nospam.oops>在消息中写道

news:c0 ********** @ linux01.dannytuppeny.com ...

从这个页面:
< a rel =nofollowhref =http://www.c-sharpcorner.com/2/mt_beginner1.asptarget =_ blank> http://www.c-sharpcorner.com/2/mt_beginner1.asp

调用线程类的Abort方法永久终止线程。确保你在Abort之前给IsAlive打电话。

if(thread.IsAlive)
{
thread.Abort();
}

我的问题是,如果线程在IsAlive被检查和Abort()被调用之间完成它正在做什么会发生什么?

线程并不是唯一的地方我见过这样的事情,而且我总是假设他们已经考虑过了(不管怎样都不可能),但我知道我们不会写应用程序通过说不,它永远不会发生和
现在我很好奇!

- 黛西奶牛



< BLOCKQUOTE>" Dave和QUOT; <无**************** @ wi.rr.com>在消息中写道

news:ek ************** @ TK2MSFTNGP09.phx.gbl ...

< snip> < blockquote class =post_quotes>你是对的;有许多代码片段忽略了竞争条件。
编写正确的多线程代码是一项非常困难的任务,即使经过彻底测试的生产代码也可能存在潜伏在其中的漏洞。
还有很多那些认为他们知道问题的人却真的



不要。


Righto。但是*你会如何*克服这样的事情?假设你想

从一个你不确定仍然存在的对象中读取一个属性。你测试它

首先存在,但你仍然有竞争条件。尝试将它包裹起来

块会起作用,但这是最好的方法吗?

-

Daisy The Cow

From this page:
http://www.c-sharpcorner.com/2/mt_beginner1.asp

Thread class''s Abort method is called to kill a thread permanently. Make
sure you call IsAlive before Abort.

if ( thread.IsAlive )
{
thread.Abort();
}

My question is, what happens if the thread finished what it was doing
between IsAlive being checked, and Abort() being called?

Threading isn''t the only place I''ve seen things like this, and I''ve always
just assumed they''d thought about it (and it was unlikely anyhow), but I
know we don''t write applications by saying "nah, it''ll never happen" and now
I''m curious!

--
Daisy The Cow

解决方案


"Daisy" <da***@nospam.oops> wrote in message
news:c0**********@linux01.dannytuppeny.com...

From this page:
http://www.c-sharpcorner.com/2/mt_beginner1.asp

Thread class''s Abort method is called to kill a thread permanently. Make
sure you call IsAlive before Abort.

if ( thread.IsAlive )
{
thread.Abort();
}

My question is, what happens if the thread finished what it was doing
between IsAlive being checked, and Abort() being called?

I don''t know what the author was thinking when he made that statement but
you are correct (IMO) for being suspicious of it. The code snippet does
nothing to detect or prevent race conditions, and I''m not sure why he feels
it necessary to test for that condition anyway. There are many reasons for
not using Thread.Abort anyway.
Threading isn''t the only place I''ve seen things like this, and I''ve always
just assumed they''d thought about it (and it was unlikely anyhow), but I
know we don''t write applications by saying "nah, it''ll never happen" and now I''m curious!


You are correct; there are many code snippets that ignore race conditions.
Writing correct multithreaded code is a very difficult task and even
thoroughly-tested production code can have threading bugs lurking in it.
There are also many people who think they know the issues but really don''t.

Dave


It has been my experience that calling Abort() on a thread that already has
been terminated doesn''t cause an exception to be thrown. Run the following
code below for an example. Calling IsAlive() is a good practice.

using System;
using System.Threading;

namespace ConsoleTester
{
/// <summary>
/// Summary description for Class1.
/// </summary>

class Test
{
public static void Main()
{
Thread newThread = new Thread( new ThreadStart( TestMethod ) );
newThread.Start();
Thread.Sleep( 1000 );

// Abort newThread.
Console.WriteLine( "Main aborting new thread." );
newThread.Abort( "Information from Main." );

// Wait for the thread to terminate.
newThread.Join();
Console.WriteLine( "New thread terminated - Main exiting." );
Console.ReadLine();
}

static void TestMethod()
{
try
{
//while( true )
{
Console.WriteLine( "New thread running." );
//Thread.Sleep( 1000 );
}
Console.WriteLine( "New thread exiting..." );
}
catch( ThreadAbortException abortException )
{
Console.WriteLine( (string) abortException.ExceptionState );
}
}
}

}

Hope this helps.
--
Jeffrey Wynn
jw***@nospam.net
"Daisy" <da***@nospam.oops> wrote in message
news:c0**********@linux01.dannytuppeny.com...

From this page:
http://www.c-sharpcorner.com/2/mt_beginner1.asp

Thread class''s Abort method is called to kill a thread permanently. Make
sure you call IsAlive before Abort.

if ( thread.IsAlive )
{
thread.Abort();
}

My question is, what happens if the thread finished what it was doing
between IsAlive being checked, and Abort() being called?

Threading isn''t the only place I''ve seen things like this, and I''ve always
just assumed they''d thought about it (and it was unlikely anyhow), but I
know we don''t write applications by saying "nah, it''ll never happen" and now I''m curious!

--
Daisy The Cow



"Dave" <no****************@wi.rr.com> wrote in message
news:ek**************@TK2MSFTNGP09.phx.gbl...
<snip>

You are correct; there are many code snippets that ignore race conditions.
Writing correct multithreaded code is a very difficult task and even
thoroughly-tested production code can have threading bugs lurking in it.
There are also many people who think they know the issues but really


don''t.

Righto. But how *would* you overcome something like this? Say you wanted to
read a property from an object you''re not sure still exists. You test it
exists first, but you''ve still got a race condition. Wrapping it in a try
block would work, but is that the best way?
--
Daisy The Cow


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

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