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?
Instead of using the stream, try using the output events !
See my answer here to my own question:
Controling cmd.exe from Winforms