How to stop a non-responsive thread

I have a file transfer application that moves files between FTP servers. Like every time we can have dozens of files in motion. To prevent floods of FTP servers, I have a monitor and semaphore locking system.

Each time, my FTP client hangs somewhere inside System.Net.Sockets.Socket.Receive () according to the call stack. I do not get an exception, so I can not solve the problem. I would like to cancel the thread as it blocks other threads that the FTP client wants to use.

I considered launching a method that ultimately calls System.Net.Sockets.Socket.Receive () on a new thread and interrupting the thread after a period of time, but I am concerned that sockets will remain open after the thread is interrupted. Is there a more graceful way to kill and cleanse after a non-responsive stream?

+5
source share
3 answers

No. There is no safe and reliable way to kill a thread without his cooperation. Existing mechanisms can be quite heavy and / or just not necessary.

  • Interrupt() , , , , / -, . -, , , , . , , "".
  • Abort(), , , - . , . (, , finally. , , , - , (, , ) .)
  • , , Thread.Abort - , , .
  • , , . , . , // , , - (, "" ).

( (ReceiveAsync), BeginReceive/EndReceive). , ( - , , , ).

+4

ReceiveTimeout?

Watchdog Watchdog, .

+2

I would suggest starting a thread in a separate process (if possible) and killing it ruthlessly. Then the OS will release all resources

0
source

All Articles