loading controls and using load, activated and sub new()
Hi,
i'm developping an application for pocket PC using VS 2005 Beta2 and here is (are?) my problem(s?)
I set up the first page as, let's say, "loading", once I launch the application, I want to first of all check if a login is set, if it is, show the login form ("login") and if not, load all the controls on my loading form, once they're all displayed, load the data from my database to finally show the welcome page ("welcome")
now, here's my method (all of them are into the "loading" form):
in the sub new() : check if the login is set, if it is, show "login" in dialog mode if not or if the login is correct, exit the sub new()
in the sub loading_load() : load all of the data from the database then show "welcome"
Now, here's the trick : "loading" only shows after all the data have been loaded, even if "welcome" is visible, it's hidden by "loading", got it? I know why it doesn't work : "loading" hasn't called its show method while "welcome" already did. So : How can I at first load all the controls in "loading" before reading my database then show the "welcome" form without it loosing focus?
One last thing : i tried to use the sub loading_activated method, but "loading" always feels like it's being activated.
[1274 byte] By [
Seta] at [2008-2-8]
This looked to be the answer to my prayers, I wanted to build a form in the background while a user is logging in so the total user experience seems quicker.
However, when I try to do this, I get an argument exception when adding controls to a panel on the form.
Any ideas? The setup function works fine if I run it inline.
Thanks
Sorry, I took so long I have been on vacation. Yes, the answer is that you can not add to the form or controls from a thread other than the main thread that the form was created on. To do this you need to create a method that will update / add controls. Then call this from the background by using this.Invoke or
control.Invoke . I will give you some code.
I do this to update a progress bar. You would create and add the controls in the following method. And call it from the background thread.
| | c# //The method that needs to run on the main thread void incrementProgressbar(int Increment) { progressBarSplash.Value += Increment; }
|
Next create a delegate that maches the above method signature.
| |
public delegate void ProgressBarDelegate(int Increment);
|
Finally call it from the background thread.
| | //The method running on the background thread void StartProgressBar() { int count = 0; double decay = 1; do { count++; Application.DoEvents(); Thread.Sleep(100); if (count >= (int)decay) { decay *= 1.3; //Call to the Main Thread progressBarSplash.Invoke(new ProgressBarDelegate (this.incrementProgressbar), new object[] { 3 }); } } while (!done); //Call to the Main Thread progressBarSplash.Invoke (new ProgressBarDelegate(this.incrementProgressbar) , new object[] { -1 }); } |