views:

49

answers:

2

I have a project that shows a UI when no proper command line arguments are passed. It's is a simple utility built to dynamically update a desktop wallpaper. When it's done with its update I call

this.Dispose();

but this causes an issue if command line arguments are passed. The form actually ends up getting the Dispose method call before it has been shown to the user. The result is a nasty error saying that the program failed to run properly even though it did just as I wanted.

I can respect the that that I'm not being very logical about this but I haven't a clue another way to look at a solution. Is there another way to call the Dispose() method cleanly before an application has loaded without causing an error?

Additional Info:

The above is evidentially not clear and I apologize for that. My program is started from a Program.cs file:

Application.Run(new MainForm());

In the constructor I check if command line arguments exists and if so call a routine called SilentRun. This private method checks if the arguments are valid and if so passes them to a method to commit actions called BrandSystem().

After BrandSystem() is complete, with its work, the last statement is

this.Dispose();

as there is nothing else for the application to actually do. This same method is called if no command arguments are passed and the user uses the GUI to apply the changes. Essentially once the process of updating the wallpaper is complete I want the application to terminate itself.

The reason for the command line arguments are so this utility of sorts can be added to an installation package and run silently in the background. My installation process does not have the native ability to update a wallpaper and this is a two-bird-one-stone utility for me.

The issue comes when the program runs in command line mode and calls Dispose before everything is actually loaded. I'm open to suggestions even if they prove my logic completely unsound.

+1  A: 

Why not just flag it, wait until it has loaded, then exit the application? Dispose is meant for a specific task: Reclaiming resources from unmanaged objects.

Rushyo
What do you mean by flag it?
Jeff
Set a boolean, store a session. Do something to 'store' the fact that the application needs to be shut down, then use that information to decide whether to shut it down at the appropriate time.
Rushyo
+1  A: 

I can't see all your code, but is there any reason your program must call Application.Run when running in silent mode? If you eliminate starting the GUI, then you shouldn't have to worry about disposing it.

Here is how I might do it:

// In main method

if (runningInSilentMode)
{
    // Run the app in silent mode
    // Do core behavior based on arguments, don't call Application.Run
}
else
{
    // Run the app in interactive mode
    // Do core behavior based on the user's input
    Application.Run(new MainForm());
}

Of course, this assumes that your core program logic is not integrated with your GUI. If it is integrated with your GUI, you will need to do some refactoring to separate it.

Zach Johnson
I think you broke the code here! Of course the answer is to move the SilentInstall() method to an external class not held with the GUI and move the command line arguments check to the Program.cs file. Thanks. I knew this was going to be a slap-the-forehead moment and, well, here I am slapping myself in the forehead. Thanks!
Jeff