Windows Develop Bookmark and Share   
 index > Windows Forms General > C# Windows Application Detect Run Mode
 

C# Windows Application Detect Run Mode

We have a C# windows application which can be executed either in UI mode or a scheduled task. When executed as a scheduled task, no UI will be displayed and all the operations will be performed as background job. I want to know whether we can determine how the application is being launched (whether as an interactive user or by the scheduled task).

Thanks in advance.

maheshgv  20 hours 18 minutes ago
The easiest option would be to have a command line switch.  Setup your scheduled task to launch your program like so:

    myProgram.exe /noui

Then just use Environment.GetCommandLineArgs() to check for the flag.  If it exists, don't show your UI.

This way, when the user just starts your program normally, it'll run with a UI.  When you schedule the task, as long as you put in the flag, it won't show the UI.

A more elaborate, but potentially nicer approach, would be to make 2 versions of your program.  Move all of your logic to a library project (DLL), and just have a exe that wraps the DLL in a UI, and an exe that wraps it in a console app.  Use the console app for your scheduled task.

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  19 hours 25 minutes ago
Reed,

Thanks for the update. I already have a commandline option that sends the name of a file which determines how to run the application (RunMode - Service or Normal). The problem is sometimes users dont setup the correct value and try to run the Normal mode as service mode. I wanted to see if I can detect such things and inform the user.
maheshgv  18 hours 25 minutes ago
Difficult to test it in all cases.  You could, potentially, try to check to see what user you're running under, and adjust accordingly, but that's prone to error, since it's possible to run a task as a user, etc.

If you're already doing this, and it's not working well enough for you, I'd suggest my second option above - move the logic to a DLL (pretty much everything except your program entry point), and have 2 executables - a UI version and a task version.  That should make it much more obvious to your users.

Reed Copsey, Jr. - http://reedcopsey.com
Reed Copsey, Jr.  17 hours 52 minutes ago
Reed,

Thanks. I'll see if that helps.
maheshgv  16 hours 43 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• select value in listbox please help urgent
• How do I set the background colour of a scrollbar?
• DropDown with colors
• Theme / Skin
• How to peek a message in message queue?
• ArgumentException on Textbox object, please help!
• how to get windows form template working?
• Assembly References and Deployment
• How do I add an item to the caption bar context menu?
• DATAGRIDVIEW COLUMN CHECK BOX