jan.vdbergh
jan.vdbergh

Reputation: 2119

Developing a scheduled task for Windows

I have to develop an application using C#.net that has to be run once a day. It only runs for at most one minute, so developing a Windows service is overkill and a scheduled task is the appropriate way.

However, I have a few questions about how the application can communicate its results:

Upvotes: 11

Views: 6244

Answers (4)

Kev
Kev

Reputation: 119806

In answer to your questions -

  1. If a task fails because it threw an unchecked exception you'll see that in the Sheduled Task viewer, there will be a 'Last Result' with a value something like 0xe0434f4d. Alternatively if you return an exit code that will be also be shown in the Last Result column of the Scheduled Task viewer.

  2. Writing to the console e.g. Console.WriteLine("blah"); won't show up anywhere. You'd need to write to the event log or to a log file.

Upvotes: 8

dummy
dummy

Reputation: 4284

Log4net is a very good, complete logging framework. I can highly recommend it.

Upvotes: 0

gimel
gimel

Reputation: 86344

If you go the Log File way, you can still use Console.WriteLine("blah");. The trick is to redirect the standard Out and Error streams:

        StreamWriter mylog = new StreamWriter("mylog.log");
        Console.SetOut(mylog);
        Console.SetError(mylog);

You also need to flush the buffers often, to make sure the log file contains current information.

            Console.Out.Flush();

This is quick and dirty, you really should use the Windows Event Log or log4net.

Upvotes: 4

paul
paul

Reputation: 13516

AFAIK the scheduler just kicks off a process. You can use the event log or another logging system to record the information you need to refer to later.

Upvotes: 0

Related Questions