I have a program that has Classes
- GUI
- Upload
- and a buffer between the 2 classes – ie used to communicate between the 2 classes .
The Upload
class uses Process
to run an command line FTP app. I want to return what output produced by the FTP app to be displayed in a textbox
in the GUI.
I have tried using the following code that has been truncated.
Upload Class (beginProcess() is a method used to start the Thread (not shown here)):
public delegate void WputOutputHandler(object sender, DataReceivedEventArgs e);
class Upload
{
private WputOutputHandler wputOutput;
beginProcess()
{
Process pr = new Process();
pr.StartInfo.FileName = @"wput.exe";
pr.StartInfo.RedirectStandardOutput = true;
pr.StartInfo.UseShellExecute = false;
pr.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pr.OutputDataReceived += new DataReceivedEventHandler(OnDataReceived);
pr.ErrorDataReceived += new DataReceivedEventHandler(OnDataReceived);
pr.Start();
pr.BeginOutputReadLine();
pr.WaitForExit();
}
public void OnDataReceived(object sender, DataReceivedEventArgs e)
{
if(wputOutput != null)
{
wputOutput(sender, e);
}
}
public event WputOutputHandler WputOutput
{
add
{
wputOutput += value;
}
remove
{
wputOutput -= value;
}
}
}
Buffer Class:
public void EventSubscriber()
{
uploadSession.WputOutput += Main.writeToTextBoxEvent;
}
Main Class:
public void writeToTextBoxEvent(object sender, DataReceivedEventArgs e)
{
if(this.textBox1.InvokeRequired)
{
MethodInvoker what now?
}
else
{
textBox1.Text = e.Data;
}
}
As you can see, when it come to the Main method's writeToTextBoxEvent, I've ran out of ideas. I'm not sure whether doing a UI update using a custom event is even the best way to do it. If someone could point me in the right direction I would be most grateful.
Best Answer
How about this:
The advantage for this method against Richard solution is you don't need to write the executing code twice (within the
BeginInvoke()
and again in theelse
path).Update
If you're on .Net 3.5 make it as an extension method:
And use it that way: