How can I execute the code after running my form?

I am really new to Windows Forms programming and not quite sure which way is right for programming.

This is my confusion.

I have one form:

public partial class ReconcilerConsoleWindow : Form { public ReconcilerConsoleWindow() { InitializeComponent(); SetLogText("Started"); } public void SetLogText(String text) { string logInfo = DateTime.Now.TimeOfDay.ToString() + ": " + text + Environment.NewLine; tbx_Log.AppendText(logInfo); } } 

And in my Program.cs class, I have the following code:

  static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); ReconcilerConsoleWindow window = new ReconcilerConsoleWindow(); Application.Run(window); if (CallSomeMethod() == true) { window.SetLogText("True"); } } } 

Now that the window has been displayed with the Application.Run command, the program stops at this point. How can I continue processing while the window is up?

The above example. My goal is to read the XMl file and display the datagridview. Subsequently, I look at the XMl file for changes, and every time a change occurs, I want to update the datagridview. However, as soon as the console pops up, how can I continue my program and make changes to the information displayed on the form on the fly?

+3
source share
5 answers

The processing that occurs after Application.Run is usually started in the Load event handler. You can easily create the Load method in Visual Studio by double-clicking any open space on the form.

It will look something like this.

  private void ReconcilerConsoleWindow_Load(object sender, EventArgs e) { if (CallSomeMethod()) { this.SetLogText("True"); } } 

The reason for this (as pointed out in several other answers) is because the main thread (the one called Application.Run(window) ) is now viewed using the Message Pump for the form. You can continue to work on this thread through messaging using form or form events. Or you can start a new thread. This can be done in the main method before you call Application.Run(window) , but most people will do this in Form_Load or the form constructor to make sure the form is configured, etc. After returning Application.Run all forms are now closed.

+8
source

Application.Run starts the Windows event loop. This cycle will not end until your form closes, and at that time everything you do with it does not matter.

If you want to do something with your form, do it in the Load event handler.

+3
source

Program.cs has no business rules, it should only call your form and display it. All downloads / updates / edits of the datagrid should be done on your Forms. You should use the events defined in the Forms class, for example: OnLoad, OnUnload, OnClose and many others, etc.

+2
source

You are missing a concept. In a Windows Forms application, the main thread is responsible for running the form.

You can always use more threads, but on Windows Forms I would recommend using the BackgroundWorker component for parallel tasks: http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx

Or a timer: http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx

+1
source

When Application.Run (window) is called, you will want to process the following things in the application window itself.

In the form code view, find the following (or add it)

 private void ReconcilerConsoleWindow_Load(object sender, EventArgs e) { //this is where you do things :) if (CallSomeMethod() == true) { window.SetLogText("True"); } } 
0
source

All Articles