【发布时间】:2018-03-03 00:52:57
【问题描述】:
我们确实有一个任务调度程序应用程序的 Execute 方法,它处理接收到的输出字符串,购买一个 Process (System.Diagnostics):
public override void Execute()
{
// ... more logic above
Process = new Process { StartInfo = { FileName = fileName, Arguments = args } };
Process.StartInfo.UseShellExecute = false;
Process.StartInfo.RedirectStandardOutput = true;
Process.StartInfo.RedirectStandardError = true;
Process.EnableRaisingEvents = true;
Process.Exited += ProcessExited;
Process.OutputDataReceived += new DataReceivedEventHandler(
delegate(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
lock (ExecutionContext)
{
ExecutionContext.AppendOutput(e.Data);
}
}
}
);
Process.Start();
Process.BeginOutputReadLine();
}
public void AppendOutput(string str)
{
// To do: Append strings to the _currentOutput within a period of time
_currentOutput += str + Environment.NewLine;
// Before doing the following
SendOutput(_currentOutput);
// Reset the variable
_currentOutput = "";
}
SendOutput 方法通过 SignalR 将输出发送到 UI。
问题在于,当进程运行一个产生多个输出的命令时,它还会对 UI 进行多次 SignalR 调用,使其锁定。
首先,我使用 setTimeout Javascript 方法解决了 UI 问题。它不再锁定,但由于一系列超时,它会延长输出的显示时间。
我认为处理这些输出的最佳方式是通过服务器端在一段时间内连接一系列输出,比如说 1 到 2 秒,然后再将它们发送到 UI。我倾向于使用 Timer 或 Task.Delay,但我无法清楚地构造它。
希望有人帮忙。
【问题讨论】:
标签: c# multithreading user-interface signalr