0

I have reserched and I realised that actually there is no way to make preemptive scheduling mechanisms in C#, am I right?
So, I have to simulate it by using cooperative scheduling mechanisms. Actually I have to make API, so my idea is to use some Token, and if that Token is set, then I have to stop currently running Task, run Task with bigger priority, and then continue stopped Task.. So, here is my question. I can not find a good way to stop Task, and continue it later, so is there any way to do that. I have one idea to do nothing in while loop until that token is set, so it will seem like I stopped that Task, and continue later (this is just simulation).. But I hope that there is some better way.. Also I have find EventWaitHandle Class, but I am not sure if I can manage with individual Threads or Tasks.

Actually, I have to make my own Scheduler (of course not something really good, just simulation).. So I have to give him Tasks with priorities, and Scheduler have to schedule that, but if in some moment I add one more Task, and if it has bigger priority, I have to stop current Task, run Task with bigger priority, and when this Task finish, I have to continue stopped Task.. So I have to know if there any good way to suspend Task (but without using Suspend function, because it can make deadlock, and not good to use)...

ProgNow
  • 45
  • 4
  • 1
    Stopping tasks is definitely [xy problem](https://meta.stackexchange.com/q/66377/299295) and "preemptive scheduling mechanisms" is probably too. What are you trying to do? Be specific please. – Sinatr Nov 26 '20 at 10:02
  • There is fine balance between posting your assignment as is and posting just an attempt... In reality both are needed to provide some sensible guidance... In this case "preemptive scheduling with Task" is very confusing request as tasks are generally used for asyncronous operations and most of the time do nothing - preempting operation that does not do anything is ... strange at least. – Alexei Levenkov Nov 26 '20 at 10:06
  • Actually, I have to make my own Scheduler (of course not something really good, just simulation).. So I have to give him Tasks with priorities, and Scheduler have to schedule that, but if in some moment I add one more Task, and if it has bigger priority, I have to stop current Task, run Task with bigger priority, and when this Task finish, I have to continue stopped Task.. So I have to know if there any good way to suspend Task (but without using Suspend function, because it can make deadlock, and not good to use)... I hope that I described better my problem, thanks. – ProgNow Nov 26 '20 at 10:09
  • This is just a college test, not something that have to work really well. Just to simulate your own scheduler (of course can use Scheduler, Tasks in C#). – ProgNow Nov 26 '20 at 10:14
  • Have a look at this [related question/discussion](https://stackoverflow.com/q/2599361/1911064). – Axel Kemper Nov 26 '20 at 10:31
  • Threads already "implement" preemtive scheduling. Tasks are conceptually not for that kind of thing - they just represent some operation which potentially can be completed in future. – Evk Nov 26 '20 at 10:35
  • Actually, at the beginning I have to specific how many threads will be used.. And then I have to simulate preemptive scheduling for example two tasks on one thread.. That is the problem, but just simulation... – ProgNow Nov 26 '20 at 10:58

0 Answers0