Jump to content

So I just made a program in C#, and it has two forms: A splash screen and then the actual useful program. Now, the splash screen does what it's supposed to, and then the second form opens up as expected. BUT, when you close the second form, the two processes don't actually end, and they just sit there and linger in RAM.

 

How in the nuts do I fix this?

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 5x 8TB WD White Label/Red (Plex) (both arrays in their respective Windows Parity storage spaces), 1TB Teamgroup MP33 (dumping ground) Corsair RM750x, TrueNAS Scale

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, MSI GTX 970 100ME, EVGA 650G1, Windows 11 Pro

OptiPlex 7040M

Spoiler

Intel Core i7 6700, 2x16GB Mushkin Redline (stuck at 2133MHz CL13), 240GB Corsair MP510, 2TB Seagate Barracuda 2.5", 130w Dell power brick, Windows 11 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Steam Deck LCD (512GB), Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB, PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to comment
https://linustechtips.com/topic/468800-program-not-properly-closing/
Share on other sites

Link to post
Share on other sites

Ah...I think I know what's happening.

By default , the exit button doesn't actually stop the process, maybe?

I don't know that much C# though.

Or you might be trying to close a wrong object.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Post the code perhaps?

 

Essentially you have something that's not being disposed of properly, likely due to incorrect logic/use.

Form1 (splash)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Afromans_Launcher{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void Form1_Load(object sender, EventArgs e)        {            timeLeft = 10;            timer1.Start();        }        private void timer1_Tick(object sender, EventArgs e)        {            if (timeLeft > 0)            {                timeLeft = timeLeft - 1;            }            else            {                timer1.Stop();                new Form2().Show();                this.Hide();            }        }        public int timeLeft { get; set; }    }}

Form2 (useful bit)

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.Drawing;using System.Text;using System.Windows.Forms;namespace Afromans_Launcher{    public partial class Form2 : Form    {        public Form2()        {            InitializeComponent();        }        private void button9_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.linustechtips.com");        }        private void button1_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.pandora.com");        }        private void button2_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.youtube.com");        }        private void button3_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.vessel.com");        }        private void button4_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.gmail.com");        }        private void button5_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.twitter.com");        }        private void button6_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.flickr.com");        }        private void button7_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.imgur.com");        }        private void button8_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.reddit.com");        }        private void button10_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.jayztwocents.com");        }        private void button11_Click(object sender, EventArgs e)        {            System.Diagnostics.Process.Start("http://www.pouringpixels.weebly.com");        }        private void Form2_Load(object sender, EventArgs e)        {        }    }}

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 5x 8TB WD White Label/Red (Plex) (both arrays in their respective Windows Parity storage spaces), 1TB Teamgroup MP33 (dumping ground) Corsair RM750x, TrueNAS Scale

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, MSI GTX 970 100ME, EVGA 650G1, Windows 11 Pro

OptiPlex 7040M

Spoiler

Intel Core i7 6700, 2x16GB Mushkin Redline (stuck at 2133MHz CL13), 240GB Corsair MP510, 2TB Seagate Barracuda 2.5", 130w Dell power brick, Windows 11 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Steam Deck LCD (512GB), Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB, PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to post
Share on other sites

As suspected, you still have a form hanging around... you hid Form1 when the timer tick event fired.

Kay. How does fix? Even when I set Hide to Close it just closes everything after the timer is up AND I have a lone process hanging around.

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 5x 8TB WD White Label/Red (Plex) (both arrays in their respective Windows Parity storage spaces), 1TB Teamgroup MP33 (dumping ground) Corsair RM750x, TrueNAS Scale

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, MSI GTX 970 100ME, EVGA 650G1, Windows 11 Pro

OptiPlex 7040M

Spoiler

Intel Core i7 6700, 2x16GB Mushkin Redline (stuck at 2133MHz CL13), 240GB Corsair MP510, 2TB Seagate Barracuda 2.5", 130w Dell power brick, Windows 11 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Steam Deck LCD (512GB), Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB, PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to post
Share on other sites

Kay. How does fix? Even when I set Hide to Close it just closes everything after the timer is up AND I have a lone process hanging around.

 

It's not really clear what you are trying to accomplish. You have one form showing another form from a timer and then you are trying to get the first form to dispose of itself. This process flow will simply not work, you will have an instance of the first form being kept alive; in the first case by calling Hide (it will still exist but be hidden) and in the second, by the way in which you have instantiated the second form:
 
An immediate and very hacky approach would be this:
        private void timer1_Tick(object sender, EventArgs e)        {            if (timeLeft > 0)            {                timeLeft = timeLeft - 1;            }            else            {                timer1.Stop();                var anotherForm = new Form2();                anotherForm.FormClosed += (o, ea) => Close();                Hide();                anotherForm.Show();            }        }
Where the second form must be responsible for closing the first/exiting the application (Application.Exit()) i.e. order of ownership.
 
I said this was hacky because it's a terrible way to be trying to show a splash screen... If you have some long running operation that you need to perform on the start-up of your application then you really want to be using the Async Await pattern, see here and here and be performing it in another thread entirely. Moreover you need to be inverting your GUI design and you need to decide where it is appropriate for you to be performing this particular loading logic. Finally you should abandon WinForms entirely and do this in WPF instead because WinForms is a deprecating technology - WPF even has a class for this right out of the box: SplashScreen; just another example of how WinForms is getting very little development attention these days.
 
If you are intent on carrying on with this in WinForms (a terrible idea imo) then you would really need to give the splash screen it's own thread, something like what is demonstrated here. Note that this example is dated because there are far better and simpler ways of achieving this now in .Net 4.5/4.6 (even using WinForms). I suspect you have tried something akin to what is shown here instead? It's terrible and a total hack; a perversion of best practices and should be thrown in the recycle bin with maximum prejudice. You are not giving things a chance to dispose/drop state correctly among other terrible things - just don't do it.

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×