ssh.net c#运行命令问题

ssh.net c#运行命令问题,c#,ssh.net,C#,Ssh.net,我在Framework3.5上的c#中使用Renci.SshNet,并在unix box上运行一个命令,如下所示 string host = "localhost"; string user = "user"; string pass = "1234"; SshClient ssh = new SshClient(host, user, pass); using (var client = new SshClie

我在Framework3.5上的c#中使用Renci.SshNet,并在unix box上运行一个命令,如下所示

        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();
        }

一切都很好,我的问题是“有没有一种方法不应该等待client.RunCommand完成”我的程序不需要unix的输出,因此我不想等待RunCommand完成。执行此命令花了2个小时,因此希望避免应用程序上的等待时间。

由于我假设SSH.NET不公开真正的异步api,您可以在线程池上排队
RunCommand

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());
    }
}
注意:如果您在WPF或WinForms中使用此选项,则需要分别使用
Dispatcher.Invoke
Control.Invoke

如何

    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;
    }

您想在运行
RunCommand()
时关闭您的应用程序,还是想防止应用程序在运行时冻结?为什么在2014年年中只限于.NET 3.5?我希望我的用户继续使用应用程序的其他区域,而不是等待2小时才能完成。目前我只能使用3.5版本,但可以升级到.NET4.OP,即3.5版本;我不相信async/await在没有一些恶作剧的情况下是可用的。谢谢。它工作得很好,你知道如何在操作完成后更新文本框值吗。。。Dispatcher.Invoke不适用于web表单…WebForms或WinForms?@MethodMan很公平,没有考虑太多:)