0

I have triying to send ssh commands to my CPU using Renci and sshnet libraries by clicking two buttons. First button, it works fine but when i clicked the second, it says

'System.ObjectDisposedException'

for client At those rows.

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

Could anyone help?

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ı");
        }
            

        


    }
}
2
  • The first command you are writing the output to a file and NOT checking the results. The second command you a piping the output to standardoutput. Then getting an error when reading results with : textBox1.Text = (cmd2.Result); Why do you have parenthesis around cmd2.Result? Is cmd2.Result a text string? Commented Feb 2, 2022 at 13:17
  • Yes, cmd2.Result is a text string. Commented Feb 3, 2022 at 13:23

2 Answers 2

1

I found the solution. When I am using "using(client)", I dispose the client. Because of disposing, button2_Click won't be able to connect the same client. My new code is below.

    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ı");
        
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

You're reusing the same client object in both button event methods. Each method contains a using statement, which will cause Dispose method to be called when control leaves the using block. A client that has already been disposed cannot be reused.

You need to either dispose the client elsewhere, or create a new client in each event handler, and dispose it as you do now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.