I made a program, which was supposed to run every night as a windows scheduled task, let's say the program is just one method, called Do_Task(). Usually you need to press a button to run the Do_Task() method, but In order for it to run as a scheduled task, I had to call the Do_Task() method in the form loading event. (Can't tell windows scheduler to press a button).

But here is the problem: when I install it by the end user, I want to have the option of either automatic (Do_Task() gets called from the from loading event), or Manuel (Do_Task() runs only from a button). In order to do testing and change settings in config file.

I tried to implement it via a check-box and an application property, to specify how the program should run the next time it runs. In the form loading event, I made an IF statement checking, if run auto is true then set application property run auto as true and run Do_Task(), etc etc.

But it didn't work, It kind of ignored me and always Ran on auto. What am I missing? I suspect (from other problems I encountered with this issue) that the form loading event is too late for these kind of stuff, so what is the earliest place you can issue commands to your program.

  • 2
    What about using a command-line-option for running unattended? Jul 16, 2015 at 23:33

1 Answer 1


Split your task logic and view logic

It sounds like your program has the task logic intermingled inside the same class which handles displaying a dialog.

A better idea would be to break this functionality up into separate classes, each of which handles one responsibility. At minimum, you should have one which handles displaying the dialog and one for performing your task (i.e., your Do_Task() method, which should also probably be renamed to something more specific about the actual task performed).

Once you split things up, you can more easily do what Deduplicator suggested in the comments: provide a command-line option for when you need to run the task unattended. It will be as simple as creating the view class and passing the task class to it for interactive mode and simply creating the task class and running Do_Task for unattended mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.