Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>System.Timers.Timer can help.</p> <p>First, basic class for task, you would probably have it by your database or some other way.</p> <pre><code>public class Task { public Task() { } public Task(String tName, Int32 iInterval, Int16 iPriority) { this.taskName = tName; this.nextInterval = iInterval; this.priority = iPriority; } private String taskName = String.Empty; public String TaskName { get { return this.taskName; } set { this.taskName = value; } } private Int32 nextInterval = 1; public Int32 NextInterval { get { return this.nextInterval; } set { this.nextInterval = value; } } private Int16 priority = 1; public Int16 Priority { get { return this.priority; } set { this.priority = value; } } } </code></pre> <p>Then the how to execute part.</p> <pre><code>public class ExecuteTasks { private System.Timers.Timer myTimer = new System.Timers.Timer(); private Int32 TaskIndex = 0; private Int32 TaskCount = 3; protected void StartExecution() { myTimer.Interval = 1; myTimer.Elapsed += new System.Timers.ElapsedEventHandler(myTimer_Elapsed); myTimer.Start(); } void myTimer_Elapsed(object sender, EventArgs e) { myTimer.Stop(); if (TaskIndex &lt; TaskCount) { Task aTask = GetTasks()[TaskIndex++]; StartTask(aTask.TaskName); SetNextTaskTimer(aTask.NextInterval); } } void SetNextTaskTimer(Int32 Seconds) { myTimer.Interval = (Seconds * 1000) - DateTime.Now.Millisecond - 1;//Interval is set in miliseconds myTimer.Start(); } void StartTask(String TaskName) { ProcessStartInfo objStartInfo = new ProcessStartInfo(); objStartInfo.FileName = TaskName; Process objProcess = new Process(); objProcess.StartInfo = objStartInfo; objProcess.Start(); } //You will be reading your tasks from your database or a file List&lt;Task&gt; GetTasks() { List&lt;Task&gt; lstTasks = new List&lt;Task&gt;(); lstTasks.Add(new Task("Task A", 2, 1)); lstTasks.Add(new Task("Task B", 10, 2)); lstTasks.Add(new Task("Task C", 60, 3)); return lstTasks.OrderBy(le =&gt; le.Priority).ToList(); } } </code></pre> <p>I have started some processes, when the given time is elapsed. (You will need to provide right path for the process to start. i.e something like</p> <pre><code>ProcessStartInfo objStartInfo = new ProcessStartInfo(); objStartInfo.FileName = "C:\\Windows\\notepad.exe"; </code></pre> <p>instead of just task name.</p> <p>And finally to trigger the execution part.</p> <pre><code>ExecuteTasks iExecute = new ExecuteTasks(); iExecute.StartExecution(); </code></pre> <p>For multithreading you can use lock mechanism, monitor classes etc, whichever suits your needs. i.e.</p> <pre><code>lock(obj)//lock on an object { ExecuteTasks iExecute = new ExecuteTasks(); iExecute.StartExecution(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload