1

I have recently been fiddling with the SSH.NET library and I've been searching/troubleshooting the disposed object error when trying to execute a second command. Here is my code:

private void statusBtn_Click(object sender, System.EventArgs e)
{
    Commands("status");
}

private void startBtn_Click(object sender, System.EventArgs e)
{
    Commands("start");
}

private void Commands(string cmd)
{
    using (sdtd_client)
    {
        sdtd_client.Connect();
        switch (cmd)
        {
            case "status":
                var status = sdtd_client.CreateCommand("7dtd.sh status Nom");
                string result = status.Execute();
                outputBox.AppendText(result);
            break;
            case "start":
                var start = sdtd_client.CreateCommand("uptime");
                string result_start = start.Execute();
                outputBox.AppendText(result_start);
            break;
            default:
                outputBox.AppendText("Unrecognized Command.");
            break;
        }
        sdtd_client.Disconnect();
    }
}

I realize "sdtd_client.Disconnect();" is disposing the connection, but even without using that, it still disposes.

How can I safely connect, execute, disconnect each time I run different commands?

Even if I opened the connection on application start and only disconnected on unload, every command executed will dispose the connection object.

Is there something obvious I am missing?

1 Answer 1

3

You haven't posted the declaration/initialisation of sdtd_client in your code, but the using (sdtd_client) block will dispose the object every time it leaves the block. I suspect you don't really want that.

Sign up to request clarification or add additional context in comments.

2 Comments

Sorry I assumed the client declaration was known -> SshClient sdtd_client = new SshClient("host", "user", "pass")
Okay, I basically just took it out of the using block and it's working.. Thanks for pointing out the obvious <3

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.