C# and TaskSchedulers

I'm working with C# trying to convert highly outdated code from VB6 (Which I have never used) to C# (2010)(More out-dated things, yay) and I'm having issues finding the appropriate code for what I am attempting to do. This code was first written when I was in 3rd grade and hasn't been touched since 2013. I am the only one working on updating it.

I'm currently looking to add Tasks to the Windows Task Scheduler to be run a specific C# file (or .exe file) daily at a time specified by the user while the computer is on. The previous VB6 code was capable of directly adding Tasks to the Window's Task Scheduler.

Please go easy on me, I have been looking for how to do this for a few weeks and have looked at most of the top results on Google when searching for related topics. Forum posts either have no explanations or use code which I apparently cannot access. The scope of this project and all its components ^(see below) is much greater than anything I've previously done. I am basically pulling my hair out now.

I have been searching and as far as I can tell the examples I have found have all used TaskScheduler as an object they instantiate, however I can not create an instance of TaskScheduler as its abstract. Both of the below aren't working:

TaskScheduler _taskScheduler = new TaskScheduler() { }; 

or

TaskScheduler _taskScheduler = new TaskScheduler(); 

I was hoping someone could answer a few simple questions for me. Or they are simple in my mind:

  1. Can I add objects directly to the Window's Task Scheduler
  2. How do you use use TaskScheduler to add Tasks to run at specified times without an instantiated instance of the object?
  3. How can you removed a specified Task from the TaskScheduler? Can it be done with just a Task Name, run Time, and file being run? I store this data in a text file so I can keep track of exactly which tasks have been created by my application.
  4. Will these C# created tasks run even when the application and its TaskScheduler object(?) are closed?
  5. Will I be able to tell which Task is being run by name? I will need to know so I can grab information from my text file mentioned above.

To explain that I have tried numerous things I'd like to point out what I've tried and hasn't worked:

  1. MSDN – I'm not exactly sure what is going on here. The documentation is just enough to give me false hope that I understand it.
  2. CodeProject – the first step is creating and instantiating an instance of a TaskScheduler object, which I am incapable of doing.

For this project I have to use WPF to create a Setup Application which lets the user automatically update the in-house programs my place of work uses. The user enters destinations for where the files are being read and then can add a schedule (Name, Time to Run, email to send to in case of errors, and options on which program to run). The program then runs at the specified time, installs the desired programs, checks they are installed with Shell(Diff) commands and then checks some additional information about the installation. If there was an error it sends a report to the specified emails. If the user specified or if there are enough errors, it will then uninstall the updates and convert to the previous version.

What I have working of the above italics is:

  • the WPF application (short of a DataGrid to show Tasks/TaskScheduler)
  • sending emails

If you're knowledgeable at helping me with any of the italic topics above I would love help with those. However the current task I am trying to tackle is this TaskScheduler.

by ZeroAurora via /r/csharp

Leave a Reply