从进程捕获输出流并在简单窗口中显示
我有点难住了。我以前曾与后台工作人员合作过,并将他们放在我的应用程序中的许多位置。为了显示正在发生的事情的“进度”,但用措辞而不是仅仅“%完成”,我创建了一个简单的“窗口”。在这个窗口中,我有一个简单的标签。由于后台工作线程与 UI 线程的问题,我无法直接访问,因此我在窗口上有一个“setter”,它的作用无非是因此
public string NewStatus
{
set { this.lblWindowShowStatus.Content = value; }
}
,当通过 BGW“ReportsProgress”并将任何消息传递给窗口的“ NewStatus”设置器(因此它在正确的 UI 线程上运行)。这一切都很完美,没有任何问题...只要后台工作人员正在 C#.Net 应用程序中执行某些操作。
接下来...我需要运行带有一些命令参数的 DOS 命令,但不想看到丑陋的黑色窗口。因此,从其他线程中,我已经找到并完美地解决了该部分。没问题。因此,此时,两个元素之间的所有语法和功能(带有后台工作人员刷新状态的窗口)和(调用 DOS 命令来运行某些参数化实用程序)。
现在,问题来了。尝试在运行 DOS 命令时刷新窗口状态。因此,这让我接触到“OutputDataReceived”来一次重定向输出和捕获行(这是完美的)。
oDOSCall.StartInfo.RedirectStandardOutput = true;
oDOSCall.OutputDataReceived += DOSOutputHandler;
// start for process and wait asynchronously until finished...
oDOSCall.Start();
// NOW begin async read of output stream
oDOSCall.BeginOutputReadLine();
oDOSCall.WaitForExit();
然后在 DOSOutputHandler 中,我让它
private void DOSOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
string txtData = outLine.Data;
// pass on to the WINDOW directly
oWnd.NewSubStatus = txtData;
}
}
挂起并且从不执行 UI 线程上窗口的更新。所以,我想...尝试将 DOS 命令包装到 BGW 线程中...所以,我这样做...几乎完全相同,所以,从我捕获其输出流的 DOS 命令中,BGW 线程获取它。然后我尝试将该字符串值转发到 Window 状态,但它似乎挂起...几乎像 UI 线程一样,调用 BGW 线程,该线程正在调用 Process(它自己的新线程)不想玩得好。
有什么想法吗?
I'm a bit stumped. I've worked with background workers before, and have them in many locations in my app. In order to show "Progress" of what is going on, but with verbiage instead of just "% complete", I've created a simple "Window". In this window, I have a simple label. Due to issues of background worker threads vs the UI thread, I can't access directly, so I have a "setter" on the Window which does nothing more than
public string NewStatus
{
set { this.lblWindowShowStatus.Content = value; }
}
So, when by BGW "ReportsProgress" and passes whatever message to the window's "NewStatus" setter (so it is operating on the proper UI thread). This all works perfectly and no problems... provided the background worker is doing something within the C#.Net app.
Next... I need to run a DOS command with some command arguments, but do not want to see the ugly black window. So, from the other threads, I've found and worked THAT part out perfectly. No problems. So, at this point, all syntax and functionality between the two elements (window with background worker refreshing the status) and (calling a DOS command to run some parameterized utility).
NOW, the problem. Trying to get the window status to refresh while running the DOS command. So, this exposed me to "OutputDataReceived" to redirect the output and capture line at a time (which is perfect).
oDOSCall.StartInfo.RedirectStandardOutput = true;
oDOSCall.OutputDataReceived += DOSOutputHandler;
// start for process and wait asynchronously until finished...
oDOSCall.Start();
// NOW begin async read of output stream
oDOSCall.BeginOutputReadLine();
oDOSCall.WaitForExit();
Then in the DOSOutputHandler, I have
private void DOSOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
string txtData = outLine.Data;
// pass on to the WINDOW directly
oWnd.NewSubStatus = txtData;
}
}
This just hangs and never performs the update to the window on the UI thread. So, I think... try wrapping the DOS command into a BGW thread... So, I do... almost all identical, SO, from the DOS command that I'm capturing its output stream, the BGW thread gets it. I then try to forward that string value up to the Window status, but it appears to hang... Almost like the UI thread, calling a BGW thread, which is calling a Process (new thread of its own) doesn't want to play well.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要使用流,而是尝试使用输出事件!
请参阅我对我自己的问题的回答:
从 Winforms 控制 cmd.exe
Instead of using the stream, try using the output events !
See my answer here to my own question:
Controling cmd.exe from Winforms