How to close splash screen?

Asked

Viewed 811 times

2

I developed a screen of splash for my application. It is working perfectly, it opens and directs to the Form2. But I realized that by closing the Form2, I have to stop debugging through Visual Studio because it seems splash is active. I used the following code below in the method Timer1:

private void timer1_Tick(object sender, EventArgs e)
        {
            if(progressBar1.Value < 100){
                progressBar1.Value = progressBar1.Value + 2;
            }
            else
            {
                timer1.Enabled = false;
                this.Visible = false;
                Form1 direcionar = new Form1();
                direcionar.ShowDialog();
            }
        }

My concern is that when installing the app on another computer, the splash will remain active in memory, in positive case, as I would do so that when closing the app, the splash would also be terminated and not just invisible.

See the image below. I closed the app, but it continues debugging.

SS

  • is why your Splash screen is just hidden, you didn’t close it. You can close the Splash screen [ . close() ] in your Form2 load event.

  • Hi Iago. I tried the way you said, but still the problem remains.

  • 1

    do the following then: use Application.Exit() in your Form2 Close event

2 answers

3


Just give a this.Close() (assuming that the screen of splash is the this) the moment you think you should. The method Close() makes the form available to be removed from memory. It does not need anything else. If this variable is no longer referenced elsewhere, at the appropriate time, Garbage colletor will release all resources that have been allocated to it. O . NET has managed memory and you do not need to worry about these things, just use right: ensure that the feature was terminated as shown above and have no more references to it that can keep you alive even if it is not shown. Would something like this:

private void timer1_Tick(object sender, EventArgs e) {
    if (progressBar1.Value < 100){
        progressBar1.Value = progressBar1.Value + 2;
    } else {
        timer1.Enabled = false;
        this.Close();
        var direcionar = new Form1(); //isto provavelmente não pode estar aqui.
        direcionar.ShowDialog();
    }
}

I put in the Github for future reference.

But only this will probably not solve the memory consumption because this object will be active preventing it from being collected. Also, I don’t remember the consequences, but if you close a form that created another one will either give error or will keep the object active.

The way you’re building the splash screen will be your main screen and will never actually be destroyed or will be destroyed by terminating the application. Or you reverse this situation, that is, create a main form and temporarily call splash screen within this form or what I would do is separate the two forms, something like that (simply speaking):

Application.Run(new SplashForm());
Application.Run(new MainForm());

One response in the OS with another way of doing . NET already has the infrastructure to perform the task.

  • Right. In this case if there is a exit button, right? But what I have is a one-screen app, where I don’t use a menu. From what I understand, the . Net will automatically close the splash once the app is closed?

  • Screen of splash is a screen that stays open for a few moments, so I do not understand this your doubt. You are responsible for closing it whenever you want. By event of a button, for a certain time, or by the end of certain operations. Maybe your problem is something else, but your question doesn’t indicate it. Or you don’t even have a screen splash and you think you have.

  • Come on bigown. I guess I wasn’t clear on my question. I created a scheduling system from which only one screen. This system has a splash screen from which it directs to another screen. When I am in Visual Studio and I click on debug the program, the splash screen appears correctly and directs to the system. So far everything OK. But when I close the app, I realize I have to stop debugging by VS as well, which didn’t happen before I put the splash. Soon I realize that the splash seems to remain active by placing this.Visible = false;.

  • What I would like to know if when running this app on another computer, this can occur, ie, the splash remain active in memory even when closing the app. If yes, how would you solve this problem.

  • This didn’t help much because you repeated what you wrote in the question and didn’t add any new information. I can’t help anymore with just this snippet of code. But the solution is the one I gave in the answer. You have to close the form, it can’t be hide.

  • Right. You’re right. I edited the code in my question. The splash remains active for a few seconds, then closes and directs to the system. So far everything is OK. But it seems that it remains active, because even closing the app, I have to stop debugging by VS.

  • Bigown, I put the image in my question. Even closing the app, the debugging remains active after I put the splash. See if the image helps my doubt.

  • This does not help anything. I improved the answer but there still seems to be a structural problem throughout the application. You should not link the creation of the application screen within the splash, they have no relationship. Should make the screen splash and only when it finishes its execution should it create a new form, in isolation. Your problem is not only in this section.

  • Right bigown. I tried using . Close(), but Form1 won’t open. Close too. You said that the Form1 direct = new Form1(); can’t be here, as I would then direct to this form after closing the splash?

  • It is what I said, you have a structural problem in the application, it is not only this. There are problems that go beyond what is set in this passage. Then there’s no way I can help more than that in this question. Look at the amount of comments, when this happens, there is something wrong in the question. The comment above gives an idea of how to solve. I do not give the complete solution because I do not know its full application.

  • Staff. The solution I found was as soon as I entered the main screen, in the Formclosed event, I put the code: Application.Exit(); Thus, when closing by the X of the main window, I also close the Splash screen that is hidden.

Show 6 more comments

0

You can put this code "Application.Exit();" at Formclosed and Formclosing to kill your Splash app for good

Browser other questions tagged

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