Archived Forum Post

Index of archived forum posts

Question:

SSH Disconnect from another thread?

May 09 '17 at 11:48

Could you explain the behaviour of the Disconnect() method of the SSH component? Will the Disconnect() method call end all other synchronous method calls?

I have a situation where the ChannelRead() method is called and waiting for a response, meanwhile the Disconnect() method is called on another thread.

It seems that the disconnect() method doesn’t complete until the ChannelRead() method completes? Is that right?

If so what is the best way to make the Disconnect() method not wait for the ChannelRead() method to complete?


Answer

Chilkat objects are thread-safe because only one method call can be active at a time on a given instance of an object. Let's say there's an object ObjA and two threads have a reference to this object. One thread calls ObjA->X and the other simultaneously calls ObjA->Y. Whichever thread was first in making the call will gain the lock on the object, and the other thread must wait. If, for example, X was called first, then Y will proceed immediately after X returns.

For some objects, like the Chilkat Socket object, where we want simultaneous calls, such as for bi-directional communications, there are workarounds. For example, the socket.CloneSocket method creates a new object that shares the same underlying connection. This allows for one thread to be writing while the other is reading simultaneously. (The thread-safety for the underlying socket is handled internally by Chilkat, but the thread-safety of the Chilkat object is satisfied because we now have two separate objects.)

Also, many property setters/getters can work without getting blocked if the object is locked by another thread. There is a special property named AbortCurrent that is common to all Chilkat classes that also have Async methods (because Async methods only exist for methods that might take some time to return). The AbortCurrent property can be set to true from the other thread. This would be the correct way to abort any running Chilkat method from another thread.