The project I am working on requires some executions to be done at a certain time. I am not sure what would be the best way to deal with this situation. The method must be able to survive server restart/maintenance. And method calls must be programmatically.
I am considering going down this path:
I could have a table in database (or even a message queue) called TaskTable which could have TaskID(PK), TaskName(varchar), TaskStatus(enum success,failed, scheduled) and TimeOfExecution. But I need a windows service that periodically polls the database for any unexecuted tasks. Problem I am facing is that: What do I use as the TaskName to save into database? Class name? class and method name ToString? And how can I convert the string back and programmatically invoke the method calls (I don’t want to have a giant switch statement)? A typtical task would look like below. So I ned to be able to get the name of the task ‘SendIncompleteNotification’ and class name save it into database and on retrival invoke programatically
public static Task<string> SendIncompleteNotification { get { return new Task<string> ( a => Console.WriteLine('Sample Task') , 'This is a sample task which does nothing.' ); } }
The problem now is I am having problem saving the method/property name progrmatically.
var type = ApplicationTask.SendIncompleteNotification.GetType(); //type.Name shows 'Task`1' rather than SendIncompleteNotification
Is there any better ways of dealing with this situation? Thanks!
Updated: Sorry my head was spinning. I now realized what I did wrong was to have another method/property to return my Task. What I should have done was to have a new class inhrite from my Task. And there i can easily get the class name and save the string into db and later retireve back and invoke.
Is the database a requirement?
If not, what about a Windows Scheduled Task (they have a tendancy to ‘just work’) which calls into a general console app. The arguments to the console app could be:
This way you can put all of your tasks into one assembly, or multiple. Alternatively you could create an attribute, apply that attribute to your tasks to give them a ‘friendly name’, and use reflection over the assembly to find classes with the matching attribute.
Edit: example:
Edit 2: This is in answer to your code sample.