3

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?

onesixtyfourth
  • 744
  • 9
  • 30

5 Answers5

4

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.

jrummell
  • 42,637
  • 17
  • 112
  • 171
  • Correct me if am wrong, but woulnd't that just exchange the WinForm window for a console window...? – Jens H Jul 19 '12 at 12:58
  • 1
    Yes, but as long as the application requires no input from the user, a Console window is fine. – jrummell Jul 19 '12 at 13:00
2

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.

manav inder
  • 3,531
  • 16
  • 45
  • 62
0

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!

Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
0

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.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
  • I've developed a winform application using VS 2010. After installing exe, I've scheduled it in task scheduler. At scheduled time, task showing running but it don't open my exe. plz help. – dilipkumar1007 Oct 02 '17 at 16:31
0

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")

Jens H
  • 4,590
  • 2
  • 25
  • 35
  • I've developed a winform application using VS 2010. After installing exe, I've scheduled it in task scheduler. At scheduled time, task showing running but it don't open my exe. plz help. – dilipkumar1007 Oct 02 '17 at 16:31
  • @dilipkumar1007: This sounds like a compeltely different topic. I suggest you better open up your own question and give some more details about what you are trying to do. – Jens H Oct 04 '17 at 07:33