先进行一个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();
}