Closing an application before it's opened.

S

ssg31415926

I've written a C# Windows Forms application. Now I've decided that it
needs to check that the user is a member of certain groups as it's
opening and close if they're not. A bit of research suggests that I
won't be able to close the application before it's finished loading.
this.Close() doesn't work in these situations. What's the recognized
way of achieving this?

I like to do things properly. If I was starting from scratch, would I
create the project as a Console Project and use something like:
Form1 form1 = new Form1();
form1.ShowDialog();
to display the form? (And find some way to hide the Console window.)

Or do I create it as a Windows Application and find run the group
membership checks as part of the first (somehow hidden) window and then
call the main window with code similar to that above?

Simon
 
C

Chris Dunaway

Can't you do it in your Main method?

[STAThread]
static void Main()
{
bool isMember;

//Code to check group membership here

if (isMember)
Application.Run(new Form1());
else
MessageBox.Show("You're not a member!");
}
 
G

Guest

You can perforam the check in the Main function of your app before the call
to Application.Run and then neglect to call Application.Run if the check
fails. This will cause the application to exit without ever showing the form.

HTH, Jakob.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top