tags:

views:

30

answers:

2

I have an application that uses a file to store its data.

I store the location of the file in the app settings so have two tests at startup:

  1. Do I have a setting for the file and
  2. Does the file (if I have a setting) exist

If I fail either test I want to prompt the user for the file location - the mechanics of the are not the problem, I can read and write the app settings, fire off dialogs and otherwise request the data. If the user refuses to choose a file (or at least a file location) I want to exit the app.

My problem is where to do this i.e. at what point in the flow of code. In an ideal world you start the app, show a splash screen, load the main form and run from there... I'm looking for a general pattern that allows me to slot the test for parameters into the right place so that I can prompt the user for whatever (and allowing that I have to worry about the fact that my splash screen is currently topmost for my app).

I appreciate that this is a bit vague so will update this with code as we go along.

+1  A: 

I would do this in the Main method, before loading the main form:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    if (VerifyPrerequisites())
    {       
        Application.Run(new MainUI());
    }
}

The method VerifyPrerequisites would do the checking of settings, files and so on and return true if the application should run.

Fredrik Mörk
+1  A: 

Fredrik Mörk's answer is fine but if you want to use application framework, you can't use sub main and have to have a startup form. A simple but very low-tech way (assuming main form is to be populated via files you want to verify):

'In the main form load: do not do anything.

.

'In the main form activated (pseudocode):
 While VerifyFiles = NotThere
  if AskForNewLocationsDialog() = Cancel
   me.close and exit sub
  end if
 end while
 Initialize form from datafiles (if that is what you need)

If you have a lot of updates and don't want them to appear, put all your controls on a panel that fills the form (dock=fill) and hide it until you're ready. You could even put a 'loading' message or progress bar in the middle of the form underneath it (but if you are loading in the GUI thread you would need throw DoEvent's in there. But since no controls but the close X are visible, closing and exiting your load loop is all you'd have to check for after the doevents).

Of course if your program is already written, and depending on what you're trying to do, this may be easier said than done; however you write as though you're just starting.

FastAl