views:

426

answers:

4

I have a C# console application that just 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 and I can open scheduled tasks and run it by right clicking and selecting run. As soon as I log out, the application ends. Any help will be greatly appreciated.

+2  A: 

Create a windows service that runs under the system account

Joel Martinez
Hi JoelThanks 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?
Peggy A
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 restart
Joel Martinez
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.
Peggy A
+4  A: 

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.

Reed Copsey
Hi ReedI am using windows task scheduler but if I am not logged into the machine where the app is scheduled, it does not run.
Peggy A
@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...
Reed Copsey
The only thing I see is the "Run only if logged on" box and it is not checked.
Peggy A
@Peggy: What OS are you using?
Reed Copsey
I am using server 2003
Peggy A
I ran it from a bat file and it ran just fine.... Thanks for you help anyway.
Peggy A
+5  A: 

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.

Jay Riggs
Hi JayThe box is unchecked and the application still ends either when I logg out. The task never starts if I am not logged in.
Peggy A
@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.
tvanfosson
Yes, I have used my credentials and I also tried the system administrator. Still the task will not run unless Iam logged in.
Peggy A
@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.
tvanfosson
I am, but the app does not get that far. Would it stop the app before it gets to the code?
Peggy A
@PeggyA - Have you implemented any sort of error logging to see how far your program gets and maybe any errors that are being thrown
Jay Riggs
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 output
Peggy A
I ran it from a bat file and it ran just fine.... Thanks for you help anyway.
Peggy A
+1  A: 

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.

tvanfosson
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.
Peggy A
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.
tvanfosson