错误:无法访问已处置的对象。对象名称:“ ShellStream”

时间:2019-07-31 12:24:22

标签: c# ssh

我正在使用C#和ASP.NET 2.0 Web应用程序。我正在尝试使用Renci SSH.NET连接到远程unix框并执行unix命令。代码运行正常,现在突然出现错误,因为-无法访问已处置的对象。我用下面的代码

public void function1()
{
 SshClient sshclient = new SshClient(hostname, username, pwd);
 sshclient.Connect();
 ShellStream stream = sshclient.CreateShellStream("cms1d", 80, 24, 800, 600, 1024);

 objCatcher.WriteOnlyInfo("whoami", stream);

 objCatcher.WriteOnlyInfo("command 2", stream);

 stream.Dispose();
 sshclient.Disconnect();
 sshclient.Dispose();
}

public void WriteOnlyInfo(string customCMD, ShellStream stream)
 {
    var writer = new StreamWriter(stream);
    writer.AutoFlush = true;

    WriteStream(customCMD, writer, stream);
}

private void WriteStream(string cmd, StreamWriter writer, ShellStream stream)
{
   writer.WriteLine(cmd);
   while (stream.Length == 0)
   {
      Thread.Sleep(2000);
   }
   Thread.Sleep(5000);
}

第一个命令正在运行,当代码执行第二个命令时,出现错误。我尝试删除RENCI dll并将其添加回去。清洁并重新构建解决方案。 对象仅放置在功能的末尾。 但这并没有帮助。如何解决这个问题?谢谢

0 个答案:

没有答案