```C#
try
{
// "server" needs to be changed to a real SSH server
// The credentials do not need to be valid
using (SftpClient client = new SftpClient("server", "user", "password"))
{
client.HostKeyReceived += delegate(object sender, HostKeyEventArgs e)
{
e.CanTrust = false;
};
client.Connect(); // <-- Exception from untrusted host key bubbles up here
} // <-- End of using block causes client.Dispose() to be called, causing an unhandled exception in another thread
}
catch (Exception ex)
{
// This code isn't executed because of the unhandled exception from another thread
Console.WriteLine("There was an error connecting to the server: {0}", ex.Message);
}
```
On my machine, this results in the following exception almost every time:
```
Renci.SshNet.Common.SshConnectionException was unhandled
HResult=-2146233088
Message=Client not connected.
Source=Renci.SshNet
StackTrace:
at Renci.SshNet.Session.SendMessage(Message message) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 658
at Renci.SshNet.Session.SendDisconnect(DisconnectReason reasonCode, String message) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 893
at Renci.SshNet.Session.RaiseError(Exception exp) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 1914
at Renci.SshNet.Session.MessageListener() in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 1594
at Renci.SshNet.Session.<Connect>b__4() in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.cs:line 529
at Renci.SshNet.Session.<>c__DisplayClass3d.<ExecuteThread>b__3c(Object o) in c:\temp\Test\Third Party\sshnet-31793\Renci.SshClient\Renci.SshNet\Session.NET40.cs:line 25
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
InnerException:
```
The problem is that `Session` is getting disposed (by the `using` statement) prior to `Session.SendDisconnect` being called from another thread while the client attempts to clean up from the exception thrown by the lack of trust in the host key.
Comments: ** Comment from web user: abeytr **
Even we are facing same issue which causes our main application to crash and stop.
Is there any work around for this?
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
```
at Renci.SshNet.Session.RaiseError(System.Exception)
at Renci.SshNet.Session.MessageListener()
at Renci.SshNet.Session.<Connect>b__4()
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
```