Relationship between the "Initializecomponent();" method and the loading of a "Splash Screen"

Asked

Viewed 322 times

3

I added a splash screen to a project in the simplest way, that is, by changing the property Build Action for Splashscreen. Although with very similar visual effects, what really happens when we compare these two solutions?

To

public MainWindow()
{
    InitializeComponent();
    Thread.Sleep(5000);
}

B

public MainWindow()
{
    Thread.Sleep(5000);
    InitializeComponent();
}

1 answer

0

In this case, it is important to know what goes on in the method InitializeComponent(). It is responsible for creating, adding and positioning all components on the screen.

So, what’s the difference between the groove To and the solution B?

In the solution To, the components are created and a Sleep() five-second after all components have been created.

In the solution B, first is given a Sleep() 5 seconds and only after that time passes will the components be created.


It is interesting to mention that Thread.Sleep() hangs the graphical interface if you are using . NET Framework 4.5 or later can test await Task.Sleep(5000);.

More information in this answer

  • So I just don’t notice the difference because, as the mainwindow is empty, it’s so fast that you can’t tell the difference between before and after?

  • You won’t notice the difference, because this Sleep runs on the constructor. Look in your Program.Cs, it creates a form (this is where Sleep will occur), and passes the form to Aplication.Run. The application that will show the form itself....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.