C#SSH命令执行

发布于 2025-01-23 21:30:46 字数 836 浏览 3 评论 0原文

我需要在我的应用程序上提供一个小帮助。当我按按钮时,应用程序会冻结,直到SSH完成发送的commnand。我尝试了从 c#应用程序冻结但是,当我尝试使用task> task.factory时,我尝试了解决方案。 startnew(()...我没有控制台的输出。有我的代码:

var cmd = SSH.CreateCommand("apt update && apt upgrade -y");
                    var asynch = cmd.BeginExecute();
                    var reader = new StreamReader(cmd.OutputStream);

                    while (!asynch.IsCompleted)
                    {
                        var result = reader.ReadToEnd();
                        if (string.IsNullOrEmpty(result))
                            continue;
                        Console.Write(result);
                    }
                    cmd.EndExecute(asynch); 

有人与它有关系吗?

i need a small help with my app. When I press the button the app freezes until ssh complete the commnand which was sent. I tried solution from C# application freezes but when I tried to use Task.Factory.StartNew(()... i dont have a output in console. There is my code:

var cmd = SSH.CreateCommand("apt update && apt upgrade -y");
                    var asynch = cmd.BeginExecute();
                    var reader = new StreamReader(cmd.OutputStream);

                    while (!asynch.IsCompleted)
                    {
                        var result = reader.ReadToEnd();
                        if (string.IsNullOrEmpty(result))
                            continue;
                        Console.Write(result);
                    }
                    cmd.EndExecute(asynch); 

Does anyone have anything to do with it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不交电费瞎发啥光 2025-01-30 21:30:46

您可能正在使用wir循环阻止您的GUI线程。

您需要运行代码异步。

public static void Main(String[] args)
{
    Task sshTask = RunCommand();
}


public static async Task RunCommand()
{
    var cmd = SSH.CreateCommand("apt update && apt upgrade -y");
    var asynch = cmd.BeginExecute();
    var reader = new StreamReader(cmd.OutputStream);

    while (!asynch.IsCompleted)
    {
        var result = reader.ReadToEnd();
        if (string.IsNullOrEmpty(result))
            continue;
        Console.Write(result);
    }
    cmd.EndExecute(asynch);
}

You are probably blocking your GUI thread with your while loop.

You need to run your code async.

public static void Main(String[] args)
{
    Task sshTask = RunCommand();
}


public static async Task RunCommand()
{
    var cmd = SSH.CreateCommand("apt update && apt upgrade -y");
    var asynch = cmd.BeginExecute();
    var reader = new StreamReader(cmd.OutputStream);

    while (!asynch.IsCompleted)
    {
        var result = reader.ReadToEnd();
        if (string.IsNullOrEmpty(result))
            continue;
        Console.Write(result);
    }
    cmd.EndExecute(asynch);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文