Avatar of Andrew Werber
Andrew Werber
 asked on

Error trying to close main form in Windows Form project

After creating a Windows Form project I added Close(); after InitializeComponent(); in the constructor. When I run the project, i get the following error:

Make sure you have not released a resource before attempting to use it

The error happens in the Program.cs file.

My real code at work is a bit more complicated, what I'm doing is authenticating a user against a table and then trying to close to form if they're not in the table. But the functions I call are called from the constructor, and the error is the same error in the same place. I'd like to know how to close the form from the constructor, other than sinking the error. I'm concerned that there may be a memory leak.
.NET ProgrammingC#

Avatar of undefined
Last Comment
Andrew Werber

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Ryan Chong

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Kyle Abrahams

You can check that before you call the constructor of the form.  Or the other option is to create a "Blackout Panel" (EG: a fully docked panel) and set that visible to true if they aren't authenticated / authorized.
Andrew Werber

ASKER
Since this was the first answer, I tried it and it worked. Thanks!
Your help has saved me hundreds of hours of internet surfing.
fblack61