Click here to Skip to main content
15,888,968 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My current Form1 is invisible when I start it.

When I start my application,
I want my Form1 being visible
then, label4 with the announcement being displayed
then, a StartPage() method that takes 5seconds to load.
and then, the label4 to dissapear.

I tried with Application.DoEvents(); and no effect.
Then I tried with Application.Run();but it is not doing what I want.

What should I use, please?
C#
private void Form1_Load(object sender, EventArgs e)
{
    Application.DoEvents();
  //  Application.Run(); //this just skip everything
    label4.Visible = true;
    StartPage();//is taking 5s to load
    label4.Visible = false;
}

Thanks.
Posted
Updated 14-Feb-16 0:20am
v2

If possible, you should remove the time-consuming code from the Load event handler. You might put it in a BackgroundWorker[^].
 
Share this answer
 
First off, as Carlo has said, you should move time-consuming work into a background thread, to free up your UI thread. Never use Application.DoEvents - if you have to, it's a good sign that you are doing something in totally the wrong way.

And I'd start it from the Form.Shown event as well rather than the Load:
C#
private void Form1_Shown(object sender, EventArgs e)
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += work_DoWork;
    work.RunWorkerCompleted += work_RunWorkerCompleted;
    label4.Visible = true;
    work.RunWorkerAsync();
    }

void work_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    label4.Visible = false;
    }

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    StartPage();
    }
 
Share this answer
 
Comments
_Q12_ 11-Aug-15 5:09am    
Thank you OriginalGriff
Your code worked as I wanted. Excellent job!
Thank you CPallini also.
OriginalGriff 11-Aug-15 5:35am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900