4

I am using Renci.SshNet in c# on framework 3.5 and running a command on unix box like below.

        string host = "localhost";
        string user = "user";
        string pass = "1234";
        SshClient ssh = new SshClient(host, user, pass);


        using (var client = new SshClient(host, user, pass))
        {
            client.Connect();


            var terminal = client.RunCommand("/bin/run.sh");

            var output = terminal.Result;

            txtResult.Text = output;
            client.Disconnect();
        }

every thing works well, my question here is that "Is there a way that it should not wait for client.RunCommand to be finish" My prog doesn't need a output from unix and hence I don't want to wait for the RunCommand to finish. This command took 2 hours to execute so wanted to avoid that wait time on my application.

3
  • You want while the RunCommand() runs to be able to close your application or you want to prevent your application from freezing while running? Commented Aug 6, 2014 at 18:54
  • 1
    Why in mid 2014 are you limited to .NET 3.5? Commented Aug 6, 2014 at 19:46
  • I want my users to keep using the other areas of application instead of waiting for for 2 hrs to complete. I am limited at the moment to 3.5 but can upgrade to .net 4. Commented Aug 7, 2014 at 14:16

2 Answers 2

1

As i assume SSH.NET doesn't expose a true asynchronous api, you can queue RunCommand on the threadpool:

public void ExecuteCommandOnThreadPool()
{
    string host = "localhost";
    string user = "user";
    string pass = "1234";

    Action runCommand = () => 
    { 
        SshClient client = new SshClient(host, user, pass);
        try 
        { 
             client.Connect();
             var terminal = client.RunCommand("/bin/run.sh");

             txtResult.Text = terminal.Result;
        } 
        finally 
        { 
             client.Disconnect();
             client.Dispose();
        } 
     };
    ThreadPool.QueueUserWorkItem(x => runCommand());
    }
}

Note if you use this inside WPF or WinForms then you will need to txtResult.Text = terminal.Result with Dispatcher.Invoke or Control.Invoke, respectively.

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

9 Comments

OP says 3.5; I don't believe async/await is available without some jiggery-pokery.
Right, didn't notice. Thanks.
it worked very well, any idea how to update the textbox value after operation completed... Dispatcher.Invoke is not available for web forms...
WebForms or WinForms?
@MethodMan Fair enough, didn't give it much thought :)
|
0

What about

    public static string Command(string command)
    {
        var cmd = CurrentTunnel.CreateCommand(command);   //  very long list
        var asynch = cmd.BeginExecute(
            //delegate { if (Core.IsDeveloper) Console.WriteLine("Command executed: {0}", command); }, null
            );
        cmd.EndExecute(asynch);

        if (cmd.Error.HasValue())
        {
            switch (cmd.Error) {
                //case "warning: screen width 0 suboptimal.\n" => add "export COLUMNS=300;" to command 
                default: MessageBox.Show(cmd.Error); break;
            }
        }

        return cmd.Result;
    }

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.