I’m trying to take a console application and build a gui for it. I’ve got the main functions of my GUI working and added the console application files to my new project. I changed the namespace of the files to match that of the winform application and moved the main function of the console application into the Program class defining it as a function taking a CSV path argument which it gets from the form.
So it appears that program it functioning but Im having trouble getting the form to update. There are some Console.WriteLine() functions that I want to write to the toolStripStatusLabel and others to the richTextBox.
I’m new to C# and the main program wasnt written by me but Im trying to build on it.
Program.cs
Form1 Frm1 = new Form1();
Frm1.UpdateStatusBar("Sorted jobs by EDD....");
Frm1.Refresh();
Form1.cs
public void UpdateStatusBar(string status)
{
Form1 Frm1 = new Form1();
Frm1.toolStripStatusLabel1.Text = status;
}
Pastebin Program.cs
See line 92.
Pastebin Form1.cs
See line 88.
As said by @Thinhbk, creating a new
Form1each time you are wanting to update the tool strip is not the way to to go. The other problem you have is that all of the processing is running in the same thread (I looked at the stuff you posted on Pastebin), meaning that you won’t see the progress updates until the end anyway.To get this working, I first modifed the signature of the
public void AX1Program(string path)method to this:Passing in the form means we can access the
UpdateStatusBarmethod andresetEventis used to signal when the thread is done. The body ofAX1Programchanges to this:Now, to invoke the
AX1Programmethod, you currently have some code (it’s in a couple of places in yourWritebutton_Clickmethod):We want to invoke this asynchronously, so we would instead go:
Which invokes the following two methods:
Because we are now wanting to update the tool strip from another thread, the
UpdateStatusBarmethod will need to look like this so that we can safely modify the controls:You could then use a similar pattern to update your rich text box.