Reputation: 49
I want to write a salesmanger for ebay and i need a basic concept how to schedule the jobs properly.
Let's say i have 2 jobs which should be performed at a specific time.
Sell item A at 15:00 Sell item B at 16:00
current time is 13:00
Currently i would do it like this: 1. sort all tasks by time 2. get time of the soonest task 3. Wait for the timedifference between the current time and the task (15:00-13:00 so for 2 hrs)
however this way is very unrealiable and bad coded (for example i start the program at 13:00 and it will wait for 2 hrs. But what if i decide to add another item for sale at 13:30 which should sell at 14:00)
I could avoid this problem by checking every second what is the next job, but I still doubt this is a good way to writte the code.
windows task scheduler is a really good example of what i need. (you set a job at a specific time and it gets executed then)
The programing language i plan to use is c#.
I am also pretty sure the question has been answered multiple times but unfortunaly i do not even know what to google for.
I hope someone could help me with some basic ideas.
Upvotes: 0
Views: 132
Reputation: 13003
From your description of the current implementation, you seems to be using Thread.Sleep to wait for 2 hours, right? If it is the case, your program is really bad coded.
Don’t sleep. Because within 2 hours your program cannot do anything, it cannot check if the job list is refreshed.
Use timers. Specifically, use System.Timers.Timer class.
Use a timer to check the job list, checking it once every second is OK. Use a longer interval like 2 or 5 seconds if you find performance issue in the timer.
For a job that needs to be done after 2 hours, just set a timer that fires after 2 hours and forget it, and remove the job from the list. If you find another job that is to be done 3 hours later, again you set another timer.
Upvotes: 1