你怎么杀死一个完全锁定的线程? [英] How do you kill a completly locked up thread?

查看:53
本文介绍了你怎么杀死一个完全锁定的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为C#没有本机SSH类,所以我使用的是SharpSSH。有时,对于

我不知道的原因,一个Connect调用将完全锁定线程并且

永远不会返回。我确信这与我正在谈论的服务器上的古怪性有关。

。无论如何,这个锁定状态发生在

时(可能每天一次)我无法弄清楚如何处理锁定的

up线程。


如果我发出一个Thread.Abort(),那么异常永远不会被抛入线程

,因为它被锁定了。这似乎是我所知道的唯一一个C#方法,即
杀死一个线程。有没有其他方法来杀死一个线程?


一种你可以自己模拟的方法,就是创建连接到

a服务器的任何线程连接需要一些时间,比如10到20秒。当

线程正在进行此连接时(即使是一个简单的TCP / IP

套接字连接也会发生)从另一个线程发出一个Thread.Abort()这使得线程对象产生了

,你会看到在Connect调用返回之前抛出的ThreadAbortException将不会是



另一种可以做到这一点的方法是在完成连接呼叫并且你开始与服务器通话后,如果你正在进行recive数据通话和服务器,那么你需要b / b
停止发送数据,但永远不会关闭连接,它将永远阻止。

你将再次无法获得Thread.Abort()杀死被锁定的

线程。


有没有人,尤其是MSVP谁能回答这个问题?

解决方案

这是一篇文章一种允许进行任何方法调用的方法

time-outable:
http://www.eggheadcafe.com/tutorials...-method-c.aspx

- 彼得

网站: http://www.eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot.com

MetaFinder: http ://www.blogmetafinder.com

" TheSilverHammer"写道:


因为C#没有本机SSH类,所以我使用的是SharpSSH。有时,对于

我不知道的原因,一个Connect调用将完全锁定线程并且

永远不会返回。我确信这与我正在谈论的服务器上的古怪性有关。

。无论如何,这个锁定状态发生在

时(可能每天一次)我无法弄清楚如何处理锁定的

up线程。


如果我发出一个Thread.Abort(),那么异常永远不会被抛入线程

,因为它被锁定了。这似乎是我所知道的唯一一个C#方法,即
杀死一个线程。有没有其他方法来杀死一个线程?


一种你可以自己模拟的方法,就是创建连接到

a服务器的任何线程连接需要一些时间,比如10到20秒。当

线程正在进行此连接时(即使是一个简单的TCP / IP

套接字连接也会发生)从另一个线程发出一个Thread.Abort()这使得线程对象产生了

,你会看到在Connect调用返回之前抛出的ThreadAbortException将不会是



另一种可以做到这一点的方法是在完成连接呼叫并且你开始与服务器通话后,如果你正在进行recive数据通话和服务器,那么你需要b / b
停止发送数据,但永远不会关闭连接,它将永远阻止。

你将再次无法获得Thread.Abort()杀死被锁定的

线程。


有没有人,特别是MSVP谁可以回答这个问题?




" TheSilverHammer" < Th ************* @ discuss.microsoft.com写在

消息新闻:7D *************** ******************* @ microsof t.com ...


因为C#没有本机SSH类,我正在使用SharpSSH。有时,对于

我不知道的原因,一个Connect调用将完全锁定线程并且

永远不会返回。我确信这与古怪的事情有关。

与我正在谈论的服务器相同的
。无论如何,这个锁定状态发生在

a

的同时(也许每天一次)我无法弄清楚如何处理

锁定

up thread。


如果我发出一个Thread.Abort(),那么异常永远不会被抛入线程

因为它被锁定了。这似乎是我所知道的唯一一个C#方法

to

杀死一个帖子。有没有其他方法来杀死一个线程?



如果你需要在运行本机代码时终止一个线程,特别是内核调用中的
,你就没办法了知道它正在修改什么状态

并保持连贯性。您必须假设整个过程已损坏。


强行结束失败线程的唯一安全方法就是结束

包含它的进程。


您是否可以访问连接的套接字句柄?如果您关闭

(通过设置SO_DONTLINGER非正常)套接字来自另一个线程

那么这可能会导致卡住操作立即完成。


哇,这些都是一些快速回复。通常我可以去几天没有

一个。


无论如何,我使用的是我没写的SharpSSH课程,但是,我做的是

有源代码,我想我可以通过它来找到插座

来电。


但是我不确定所有的地方其中发生了僵局,因此它很难解决所有问题。在某些情况下,我认为

连接成功,并且在我的对象的该实例的主线程中运行

的接收数据回调中可能发生锁定(与SSH对象)。

生病看看蛋头咖啡馆?解决方案,但我不确定它是如何适用的。

可以是锁定的所有实例。例如,如果你的类中的事件处理程序

已被另一个对象调用(IE:SharpSSH asynch回调

对于收到的数据)你不能包装在方法调用中可以超时

你能吗?


Ben Voigt [C ++ MVP]"写道:


>

" TheSilverHammer" < Th ************* @ discuss.microsoft.com写在

消息新闻:7D *************** ******************* @ microsof t.com ...


因为C#没有本机SSH类,我正在使用SharpSSH。有时,对于

我不知道的原因,一个Connect调用将完全锁定线程并且

永远不会返回。我确信这与古怪的事情有关。

与我正在谈论的服务器相同的
。无论如何,这个锁定状态发生在

a

的同时(也许每天一次)我无法弄清楚如何处理

锁定

up thread。


如果我发出一个Thread.Abort(),那么异常永远不会被抛入线程

因为它被锁定了。这似乎是我所知道的唯一一个C#方法

to

杀死一个帖子。有没有其他方法来杀死一个线程?



如果你需要在运行本机代码时终止一个线程,特别是内核调用中的
,你就没办法了知道它正在修改什么状态

并保持连贯性。您必须假设整个过程已损坏。


强行结束失败线程的唯一安全方法就是结束

包含它的进程。


您是否可以访问连接的套接字句柄?如果您关闭

(通过设置SO_DONTLINGER非正常)套接字来自另一个线程

那么这可能会导致卡住操作立即完成。


Because C# has no native SSH class, I am using SharpSSH. Sometimes, for
reasons I do not know, a Connect call will totally lock up the thread and
never return. I am sure it has something to do with weirdness going on with
the server I am talking to. Anyhow, this locked up state happens once in a
while (maybe once per day) and I can''t figure out how to deal with the locked
up thread.

If I issue a Thread.Abort() the exception never gets thrown in the thread
because it is locked up. This seems to be the only C# method I know of to
kill a thread. Is there some other way to kill off a thread?

A way you can simulate this yourself, is create any thread that connects to
a server where the connection takes some time, like 10 to 20 seconds. When
the thread is doing this connect (it will happen with even a simple TCP/IP
socket connect) issue a Thread.Abort() from another thread (the one that made
the Thread Object) and you will see that the ThreadAbortException will NOT be
thrown until the Connect call returns.

Another way you can do this is after the connect call is finished and you
start to talk to a server, if you are on a recive data call and the server
stops sending data but never closes the connection, it will block forever.
You will once again not be able to get Thread.Abort() to kill the locked up
thread.

Is there anyone, especially a MSVP who can answer this?

解决方案

Here is an article with an approach that allows to make any method call
"time-outable":
http://www.eggheadcafe.com/tutorials...-method-c.aspx
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"TheSilverHammer" wrote:

Because C# has no native SSH class, I am using SharpSSH. Sometimes, for
reasons I do not know, a Connect call will totally lock up the thread and
never return. I am sure it has something to do with weirdness going on with
the server I am talking to. Anyhow, this locked up state happens once in a
while (maybe once per day) and I can''t figure out how to deal with the locked
up thread.

If I issue a Thread.Abort() the exception never gets thrown in the thread
because it is locked up. This seems to be the only C# method I know of to
kill a thread. Is there some other way to kill off a thread?

A way you can simulate this yourself, is create any thread that connects to
a server where the connection takes some time, like 10 to 20 seconds. When
the thread is doing this connect (it will happen with even a simple TCP/IP
socket connect) issue a Thread.Abort() from another thread (the one that made
the Thread Object) and you will see that the ThreadAbortException will NOT be
thrown until the Connect call returns.

Another way you can do this is after the connect call is finished and you
start to talk to a server, if you are on a recive data call and the server
stops sending data but never closes the connection, it will block forever.
You will once again not be able to get Thread.Abort() to kill the locked up
thread.

Is there anyone, especially a MSVP who can answer this?



"TheSilverHammer" <Th*************@discussions.microsoft.comwrote in
message news:7D**********************************@microsof t.com...

Because C# has no native SSH class, I am using SharpSSH. Sometimes, for
reasons I do not know, a Connect call will totally lock up the thread and
never return. I am sure it has something to do with weirdness going on
with
the server I am talking to. Anyhow, this locked up state happens once in
a
while (maybe once per day) and I can''t figure out how to deal with the
locked
up thread.

If I issue a Thread.Abort() the exception never gets thrown in the thread
because it is locked up. This seems to be the only C# method I know of
to
kill a thread. Is there some other way to kill off a thread?

If you need to terminate a thread while it''s running native code, especially
inside a kernel call, you have no way of knowing what state it is modifying
and keeping it coherent. You have to assume the whole process is corrupted.

The only safe way to forcibly end a failed thread like you have is to end
the process containing it.

Do you have access to the socket handle for the connection? If you shutdown
(non-gracefully by setting SO_DONTLINGER) the socket from a different thread
then that will probably cause the stuck operation to complete immediately.


Wow, these are some fast replies. Normally I can go several days without
one.

Anyway, I am using the SharpSSH class which I did not write, however, I do
have the source code and I suppose I could dig through it to find the socket
calls.

However I am not sure where all the deadlocks are happening in it so it
would be very hard to catch all the problems. In some cases I think the
connect succeeds and a lockup may occur in a receive data callback which runs
in the main thread of that instance of my object (opposed to the SSH object).
Ill look at the egg-head caf?? solution, but I am not sure how applicable it
can be to all the instances of a lockup. For example, if an event handler
in your class has been called by another object (IE: SharpSSH asynch callback
for on data received) you can''t wrap that in a method call that can time out
can you?

"Ben Voigt [C++ MVP]" wrote:

>
"TheSilverHammer" <Th*************@discussions.microsoft.comwrote in
message news:7D**********************************@microsof t.com...

Because C# has no native SSH class, I am using SharpSSH. Sometimes, for
reasons I do not know, a Connect call will totally lock up the thread and
never return. I am sure it has something to do with weirdness going on
with
the server I am talking to. Anyhow, this locked up state happens once in
a
while (maybe once per day) and I can''t figure out how to deal with the
locked
up thread.

If I issue a Thread.Abort() the exception never gets thrown in the thread
because it is locked up. This seems to be the only C# method I know of
to
kill a thread. Is there some other way to kill off a thread?


If you need to terminate a thread while it''s running native code, especially
inside a kernel call, you have no way of knowing what state it is modifying
and keeping it coherent. You have to assume the whole process is corrupted.

The only safe way to forcibly end a failed thread like you have is to end
the process containing it.

Do you have access to the socket handle for the connection? If you shutdown
(non-gracefully by setting SO_DONTLINGER) the socket from a different thread
then that will probably cause the stuck operation to complete immediately.


这篇关于你怎么杀死一个完全锁定的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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