Newbie Question
I come from a java background. I am currently developing a mailer program.
What I want to happen is when the application starts the GUI must FIRST display and then the mails must be sent and the GUI gets updated as this takes place.
The app is sending the mails but before the gui gets displayed. I think the problem is as follows. In my main method is the following code
myMailer.Mail();
Application.Run(myMailer);
If I put the Mail myMailer.Mail(); after the Application.Run(myMailer); the mails will only send when I close the application.
In java you just run the contructor and then chuck in the methods and it works. What should I do here?
Thanks
[752 byte] By [
testvoid] at [2007-12-16]
Hi,
Ok, I guess your quite confused here. If you want to display the form first an then process the mails, you could take advantage of you form's
Load event. This event fires after your form is completely loaded. Or just as you say, you could place it in the
constructor of your form....
cheers,
Paul June A. Domag
According to the documentation
Load event Occurs
before a form is displayed for the first time.
I added the following method to my code. Mailer is the name of the form.
private void Mailer_Load(object sender, System.EventArgs e) {
myMailer.Mail();
}
Is this what you meant? Becuase it is still first sending the emails and then displaying the form.
When does the form actually get displayed? Is it when the call is made to Application.Run()?
That's correct. The form is actually displayed AFTER form load is done. The Activate Event is fired AFTER the form is displayed so you could do it there, but watch out because Activate is called every time the form gets focus. I haven't tried this and I don't have VS in front of me, but you may be able to just call Application.DoEvents(); before your Mail call in the Load Event (if that's all the code you have in there). But unless your Mail method spins up another thread to do the work or sends it off to another machine to do the work, it will "lock" the UI from refreshing until it's done.
Hi,
I guess the right event would be the
Shown event. Then event is fired after the form is first showed...
I agree with erik's suggestion. But since your using C# (I assume with the syntax that your using) I suggest you create a thread in the shown event and declare your mailing in that thread. But windows forms are not thread safe, so just lookup into
this.Invoke() to make it thread safe...
cheers,
Paul June A. Domag
Thanks a lot. I got the thing sorted out.
Couldn`t use the Shown method becuase I am coding with the 1.1 framework. I should have mentioned that.
I use the Activated event to start of my thread and then just use a flag so it doesnt start the thead more than once.