How do I run my .NET application in Windows Task Scheduler when I am not logged in
Asked Answered
E

5

5

I have a C# console application that processes data. The problem is that when I log off, the console application ends. If I schedule the application to run at a set time and I am not logged in, the application does not run at all. I can run it interactively by double clicking the .exe file, and I can open Task Scheduler and run it by right clicking and selecting run. As soon as I log out, the application ends.

Eckmann answered 6/4, 2010 at 17:13 Comment(0)
S
8

For XP/Windows Server 2003 open your Scheduled Task and make sure the 'Run only if logged on' checkbox is cleared.

For Windows 2008 select the 'Run whether the user is logged on or not' option.

Shea answered 6/4, 2010 at 17:16 Comment(7)
Hi Jay The box is unchecked and the application still ends either when I logg out. The task never starts if I am not logged in.Eckmann
@PeggyA -- have you set it up to run with your credentials, i.e., set the user for Run As and set it with your password. You'll need to update the task with your new password whenever you change it.Batter
Yes, I have used my credentials and I also tried the system administrator. Still the task will not run unless Iam logged in.Eckmann
@PeggyA -- I use the scheduled task stuff quite a bit and have never had a problem getting it to run when I'm not logged in using both my credentials and service ids created specifically for the job. I suspect you have something else going on in your program. Are you trying to access anything that needs mapped file resources? These are most likely not available and you should use UNC paths instead.Batter
I am, but the app does not get that far. Would it stop the app before it gets to the code?Eckmann
@PeggyA - Have you implemented any sort of error logging to see how far your program gets and maybe any errors that are being thrownShea
I wrapped a try catch around the code in my Main module to write to a text file in the try part when the app starts and the exception error to a text file in the catch part. I do not get any output when the app is started from scheduler but running interactive I do get outputEckmann
O
5

Set up the application to run using the Windows Task Scheduler. This can allow an application to run whether or not a user is logged in.

Ommatidium answered 6/4, 2010 at 17:15 Comment(4)
Hi Reed I am using windows task scheduler but if I am not logged into the machine where the app is scheduled, it does not run.Eckmann
@Peggy: Turn ON the "Run whether user is logged on or not" option in Task Scheduler. This will make it run no matter whether you're logged in...Ommatidium
The only thing I see is the "Run only if logged on" box and it is not checked.Eckmann
@Peggy: What OS are you using?Ommatidium
S
2

Create a windows service that runs under the system account

Sciomancy answered 6/4, 2010 at 17:13 Comment(3)
Hi Joel Thanks for the info, this app is huge and I am fairly new to C#. I wrote it as a console app and kick off the processing in the main module. Do you have any suggestions on how to convert it to a service? Can I just create a service app and copy my code to the service application?Eckmann
A service basically gives you 2 methods: Start, and Stop. What you can do is in the Start method, create a timer, and on timer's event (which you can configure to run every X amount of hours ... 24 for once a day for example) ... execute the code you currently have in your console app. of course you can get more complicated and say only run this at 5 pm, etc. ... but depending on your needs this may be enough. The good thing about services is that you can configure them to start automatically, so if your computer inadvertantly is restarted, you will be fine because the service will restartSciomancy
Thanks Joel, I should have written a service to begin with. I am somewhat new to .net so I'm learning. I got it to run though, I ran it from a bat file and it ran just fine.... Thanks for you help anyway.Eckmann
B
1

If I have a long-running task that is still working when I need to leave, I simply lock the screen using Windows-L and leave myself logged in. If this works, it's a much simpler solution than setting up a scheduled task every time you want to run a particular job, especially if you need to supply different parameters each time.

I realize that this may not always be possible -- you may have corporate rules that disallow this or may be using a shared computer where the next person needs access to the console (though running a scheduled task in the background surely won't make them any happier), but I offer it as a simple solution for most single-user computer scenarios. IMO, scheduled tasks are best used for just that, scheduled tasks, i.e., something you want to automate to run on a regular basis. If this describes your scenario, then by all means use one. Likewise, if what you really have is a service that runs continually and responds to requests, then use a Windows Service. If you just want to have your job keep running and secure your computer until it finishes, then locking the computer is the best way to go usually.

Batter answered 6/4, 2010 at 17:21 Comment(2)
Thanks for your info. It sounds like I need the service. This is an application that needs to run unattended twice each day at a specific time to analyze data and kick out an updated table.Eckmann
Actually, it sounds to me like you need a scheduled task. If you write it as a service, you'll need to write the scheduling code yourself.Batter
R
0

I have very similar situation. Try to set "start in" parameter in Action tab (Properties of task). Of course You should type in path to Directiory where You have Your application.

Rare answered 29/8, 2013 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.