我使用Renci.SshNet通过ssh连接到服务器。
using (var client = new SshClient("server.com", "", ""))
{
client.Connect();
var cmd = client.CreateCommand("do soemthing");
var result = cmd.Execute();
statusWindow.Items.Add(result);
var reader = new StreamReader(cmd.ExtendedOutputStream);
using (StringReader readerIt = new StringReader(reader.ReadToEnd()))
{
string line;
while ((line = readerIt.ReadLine()) != null)
{
statusWindow.Items.Add(line);
}
}
client.Disconnect();
}
它工作正常,但在尝试将此代码添加到后台工作程序时,我收到以下错误;
类型' System.Net.Sockets.SocketException'的例外情况发生在System.dll中但未在用户代码中处理
附加信息:引用的对象类型
不支持尝试的操作
这是后台工作人员代码;
public void connectBtn_Click(object sender, EventArgs e)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_Changed);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
}
public void worker_DoWork(object sender, DoWorkEventArgs e)
{
using (var client = new SshClient("server.com", "", ""))
{
client.Connect();
var cmd = client.CreateCommand("do soemthing");
var result = cmd.Execute();
statusWindow.Items.Add(result);
var reader = new StreamReader(cmd.ExtendedOutputStream);
using (StringReader readerIt = new StringReader(reader.ReadToEnd()))
{
string line;
while ((line = readerIt.ReadLine()) != null)
{
statusWindow.Items.Add(line);
}
}
client.Disconnect();
}
}
它在client.Connect();
上引发了异常为什么会出现这种情况有一个简单的原因?
编辑:我完全不知道为何会出现这种情况,我们将非常感谢任何帮助!