【问题标题】:System.ObjectDisposedException C# Ssh projectSystem.ObjectDisposedException C# Ssh 项目
【发布时间】:2022-02-03 21:31:51
【问题描述】:

我尝试通过单击两个按钮使用 Renci 和 sshnet 库向我的 CPU 发送 ssh 命令。第一个按钮,它工作正常,但当我点击第二个时,它说

'System.ObjectDisposedException'

为客户在那些行。

client.Connect();
shCommand cmd3 = client.RunCommand("ls -la checkEth*");

有人可以帮忙吗?

public partial class Form1 : Form
{
    SshClient client = new SshClient("192.168.1.5", "deneme", "deneme");
    int milliseconds;
    
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Button 1 clicked");
        using (client)
        {
            milliseconds = 200;
            Thread.Sleep(milliseconds);
           
            
                client.Connect();
            SshCommand cmd = client.RunCommand("ls > checkEth.txt");
                System.Diagnostics.Debug.WriteLine("chechethYapildi 1: " + cmd.Result);
                
                Thread.Sleep(milliseconds);
                SshCommand cmd2 = client.RunCommand("ls -la checkEth*");
            System.Diagnostics.Debug.WriteLine("com 1:" + cmd2.Result);
            textBox1.Text = (cmd2.Result);

            System.Diagnostics.Debug.WriteLine("cevapgelecek");
            
            //client.Dispose();
               
        }
        client.Dispose();

    }

    private void button2_Click(object sender, EventArgs e)
    {
        using (client)
        {
            client.Connect();
            SshCommand cmd3 = client.RunCommand("ls -la checkEth*");
            textBox1.Text = (cmd3.Result);
            System.Diagnostics.Debug.WriteLine("ikinci butona basıldı");
        }
            

        


    }
}

【问题讨论】:

  • 将输出写入文件而不检查结果的第一个命令。第二个命令将输出通过管道传输到标准输出。然后在读取结果时出错: textBox1.Text = (cmd2.Result);为什么 cmd2.Result 周围有括号? cmd2.Result 是文本字符串吗?
  • 是的,cmd2.Result 是一个文本字符串。

标签: c# ssh lib


【解决方案1】:

我找到了解决方案。当我使用“使用(客户端)”时,我会处理客户端。由于配置,button2_Click 将无法连接同一个客户端。我的新代码如下。

    public partial class Form1 : Form
{
    SshClient client = new SshClient("192.168.1.5", "deneme", "deneme");
    int milliseconds;
    
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Console.WriteLine("Button 1 clicked");
        
            milliseconds = 200;
            Thread.Sleep(milliseconds);
           
            
                client.Connect();
            SshCommand cmd = client.RunCommand("ls > checkEth.txt");
                System.Diagnostics.Debug.WriteLine("chechethYapildi 1: " + cmd.Result);
                
                Thread.Sleep(milliseconds);
                SshCommand cmd2 = client.RunCommand("ls -la checkEth*");
            System.Diagnostics.Debug.WriteLine("com 1:" + cmd2.Result);
            textBox1.Text = (cmd2.Result);

            System.Diagnostics.Debug.WriteLine("cevapgelecek");
    }

    private void button2_Click(object sender, EventArgs e)
    {
            SshCommand cmd3 = client.RunCommand("ls -la checkEth*");
            textBox1.Text = (cmd3.Result);
            System.Diagnostics.Debug.WriteLine("ikinci butona basıldı");
        
    }
}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案2】:

您在两个按钮事件方法中重用了相同的 client 对象。每个方法都包含一个 using 语句,这将导致在控制离开 using 块时调用 Dispose 方法。已经废弃的客户端不能再使用。

您需要在其他地方处置客户端,或者在每个事件处理程序中创建一个新客户端,然后像现在一样处置它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    • 1970-01-01
    • 2020-12-08
    • 1970-01-01
    • 2011-05-03
    • 2011-10-16
    • 1970-01-01
    相关资源
    最近更新 更多