I\'m really new to Windows Forms programming and not quite sure what\'s the right way to go about programming.
This is my confusion.
I have a single form:
Processing that occurs after Application.Run
is usually triggered in the form's Load
event handler. You can easily create a Load
method in Visual Studio by double clicking any open space on the form.
This would look something like this.
private void ReconcilerConsoleWindow_Load(object sender, EventArgs e)
{
if (CallSomeMethod())
{
this.SetLogText("True");
}
}
The reason this is (as stated in several other answers) is that the main thread (the one that called Application.Run(window)
) is now taken up with operating the Message Pump for the form. You can continue running things on that thread through messaging, using the form's or forms' events. Or you can start a new thread. This could be done in the main method, before you call Application.Run(window)
, but most people would do this in Form_Load
or the form constructor, to ensure the form is set up, etc. Once Application.Run
returns, all forms are now closed.
You are missing the concept. In a Windows Forms Application, your Main Thread is responsible for running the Form.
You can always use more Threads, but in Windows Forms I would recommend using a 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
Once Application.Run(window) is called, you'll want to handle subsequent things within the application window itself.
In the code view of the form, 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");
}
}
Program.cs is not meant to have business rules, it should only call your Form and display it. All datagrid loading/refreshing/editing should be done at your Forms. You should be using the Events defined on Forms class, like: OnLoad, OnUnload, OnClose and many others etc.
Application.Run
starts the Windows event handling loop. That loop won't finish til your form closes, at which time anything you do to it won't matter anyway.
If you want to do something with your form, do it in the form's Load
event handler.