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

问题描述

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

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

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

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

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

up线程。


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

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


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

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

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

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

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



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

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

线程。


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

解决方案




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

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


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

包含它的进程。


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

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

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




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

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


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

包含它的进程。


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

(通过设置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?

解决方案


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.



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.


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

09-03 04:41