3
votes

I made a simple c# .net console application. It's a backup script to copy a file to another folder and add the date to the the filename to keep it unique. The script runs perfectly.

However when I schedule it in Windows task scheduler, its status will stay on "running" after started while the application closed already.

I tried ending the main function with an int return value of 0 and I tried to have "System.Environment.Exit(0);" as last line of the application. I also removed all other code just leaving the exit code on to make sure the error was not in my code.

The checkbox "If the running task does not end when requested, force it to stop" is enabled.

All this didn't solve the problem.

I'm running windows server 2016

Anyone know how I can solve this problem?

2
Does that happen to a simple batch file containing EXIT ? If not you could start your application with that batch file and EXIT afterwards. Also hit the refresh button a few times before checking on the state, afaik it doesn't update automatically. - sknt
@MarkBeijens Can you share the Main() method? - Amey Kamat
You do realize that you have to hit F5 or refresh in the Task scheduler to update the view? - Dávid Molnár
Yeah F5 was the solution. - Mark Baijens

2 Answers

7
votes

According to this discussion on TechNet, it may help to wrap your exe in a batch file.

In my experience however, the Task Scheduler view must be actively refreshed, for example by pressing F5. This workaround is also the solution for a similar question on SuperUser.

1
votes

Probably there is a problem in your application. You can try to hunt down the issue like this:

  1. Create a completely new empty console application.
  2. Create a new Task in the Task scheduler. Do not setup any triggers, options etc. Just add the action to run the console application.
  3. Test it out - it should work without any problem.
  4. Step by step add new features and then test.
  5. Once you encounter a problem, you know what is causing it.

I did 1-3 on Windows 10 and it is working like expected.