onesixtyfourth
onesixtyfourth

Reputation: 864

Run C# application as scheduled task

I have a C# application that was designed to run with a windows form but now needs to run as a scheduled task. I have had problems with this and I think it is because it needs to be "headless" in that it should have no concept of a user environment. The program has been written to run unattended in that it has an /AUTO arg which then will run from some defaults but the form is still shown which causes the problem.

I have looked around and I think there is a way to suppress the form in this situation but I can't find exactly how. Does anyone know how I can suppress the form and allow this application to run?

Upvotes: 3

Views: 9333

Answers (5)

Jens H
Jens H

Reputation: 4632

As far as I understood you, you do not need the Forms mode anymore, right?

If this is correct, I suggest implementing your application as service, e.g. a WCF service. It can permanently run and execute your business logic on a configured timer.

To make configuration of the timer easy and flexible, you could optionally imncorporate NCronTab. This allows you to schedule the task in a pattern as easy as 45 11 * * Friday (=> "Run every Friday at 11:45 am")

Upvotes: 0

manav inder
manav inder

Reputation: 3601

Convert your application to console mode and also check the "Hidden" checkbox in the 'General' tab of Task scheduler.

This will help you

enter image description here

Still if you want to use the WinForm application, then set its ShowOnTaskbar property to false.

And its very easy to convert your winform application to winform one. Just go to the project properties and change the output type to Console. But you need to do few tweaks in the code.

Upvotes: 2

BugFinder
BugFinder

Reputation: 17858

You can tell the task to interact with desktop in which case a form that shows is not a problem. As long as your app will close by itself so the job finishes..

I made a form based thing and then wanted a scheduled task so wrote a commandline front end calling the form based app, and then pushed the 2 exes together with ilmerge so it cant get confused, because it was a cheap hack

If your code is properly written you can do an exe front end and use the same classes as your form (or dll) and work that way.

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 33993

Check if the /AUTO parameter is set and then depending on whether or not is is. Change this in your Program.cs:

Application.Run(new Form1());

to

Application.Run();

this won't show a form and you can do whatever other things you like.

This is the best I can give you without having seen you code. Hop it helps!

Upvotes: 0

jrummell
jrummell

Reputation: 43067

Take the logic required for the scheduled task out of your WinForms application and put it in a Console application. If you can reuse logic both places, move it into a shared library.

Upvotes: 4

Related Questions