Cipher
Cipher

Reputation: 6092

Two kinds of startup behaviour of application

I have an application for which I want to have two different kinds of startup behaviour.
For eg: If the user runs the application from the desktop or from application shortcuts, the application should run and ask for input.

However, my application also gets set as startup application. And if the application is started automatically on computer restart, it should not ask for user input and have a different flow (because it will have remembered preferences)

How can this kind of behavior be achieved? Probably, I was thinking to have two different constructor overloads, which will do different things in tow cases. However, how do I choose which constructor to run at appropriate time (startup app run/desktop app run)?
How can this be done in WPF?

Upvotes: 0

Views: 105

Answers (2)

Vladislav Zorov
Vladislav Zorov

Reputation: 3056

I have done this before with different classes, because the behavior varied greatly depending on the configuration and I wanted to totally isolate the cases from one another.

After you have parsed your command line arguments, you can instantiate a class and call a method with:

string customerClassName = string.Format("DataProcessor.{0}Processor", ConfigurationManager.AppSettings.Get("Customer"));
Type customerClass = Assembly.GetExecutingAssembly().GetType(customerClassName);
ConstructorInfo ctor = customerClass.GetConstructor(System.Type.EmptyTypes);
Logger.Log("Instantiating class " + customerClassName);
object instance = ctor.Invoke(null);
customerClass.GetMethod("Run").Invoke(instance, new object[] { args });

In the GetConstructor() you can specify different constructor overloads, in my case constructor without parameters.

Upvotes: 0

Luis Filipe
Luis Filipe

Reputation: 8708

Have you considered using start up arguments. When you run it automatically you could put an argument

"MyApplication.exe -autostart"

When installing the shortcuts the application can start with no arguments

"MyApplication.exe"

In your application "Main" method use an if clause and react accordingly. Do to do so, inspect the argument "string[] args" from the main method

static void Main(string[] args)

Upvotes: 5

Related Questions