大果子你说谁呢 2023-10-31 10:04 采纳率: 65%
浏览 34
已结题

c# wpf执行函数时,界面卡死

先进行一个button函数,再包含GetLog这个函数,处理数据时界面卡死

     public WPFWindow()
        {
            InitializeComponent();
        }
        private void btnClick(object sender, RoutedEventArgs e)
        {
         GetLog(fromtime, totime);
        }
        private static void GetLog(string fromtime, string totime)
        {
            SshClient sshClient = GetSSHClient();
            try
            {
                sshClient.Connect();
                if (sshClient.IsConnected)
                {
                    string datname = fromtime + ".dat";
                    TextList = String.Format(System.IO.File.ReadAllText(System.Threading.Thread.GetDomain().BaseDirectory.TrimEnd('\\') + "\\text.txt"));
                    using (ShellStream shell = sshClient.CreateShellStream("", 0, 0, 0, 0, 0))
                    {
                        Console.WriteLine(SendCommand("su - root", shell));
                        Thread.Sleep(500);
                        Console.WriteLine(SendCommand("root", shell));
                        Console.WriteLine(SendCommand("cd xxx", shell));
                        Console.WriteLine(SendCommand(TextList, shell));//下载log
                        Thread.Sleep(20000);
                        Console.WriteLine(SendCommand(string.Format("cp -rp {0} /home/xxx/",  datname), shell));
                        Console.WriteLine(SendCommand(string.Format("rm {0}",  datname), shell));
                        Console.WriteLine(SendCommand("rm *.dat", shell));
                        shell.Close();
                        sshClient.Disconnect();
                        sshClient.Dispose();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public static string SendCommand(string cmd, ShellStream sh)
        {
            StreamReader reader = null;
            try
            {
                reader = new StreamReader(sh);
                StreamWriter writer = new StreamWriter(sh);
                writer.AutoFlush = true;
                writer.WriteLine(cmd);
                while (sh.Length == 0)
                {
                    Thread.Sleep(500);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("exception: " + ex.ToString());
            }
            return reader.ReadToEnd();
        }

  • 写回答

1条回答 默认 最新

  • 赵4老师 2023-10-31 10:18
    关注

    将耗时的操作放在另一个线程里运行

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 10月31日
  • 修改了问题 10月31日
  • 创建了问题 10月31日