using System; |
| using System.Collections.Generic; |
| using System.Linq; |
| using System.Text; |
| using System.Threading.Tasks; |
| using System.Threading;
|
| |
| namespace TimerLesson |
| { |
| public class Program |
| { |
| static void Main(string[] args) |
| { |
| Program prg = new Program(); |
| |
| Thread.Sleep(5000); |
| } |
| |
| Queue<TimerTask> timerTaskQueue = new Queue<TimerTask>(); |
| |
| Timer timer; |
| |
| public Program() |
| { |
| timer = new Timer(new TimerCallback(Timer_Tick)); |
| |
| timerTaskQueue.Enqueue(new TimerTask { WaitTime_msec = 1000, Message = "first task" }); |
| timerTaskQueue.Enqueue(new TimerTask { WaitTime_msec = 500, Message = "sencond task" }); |
| timerTaskQueue.Enqueue(new TimerTask { WaitTime_msec = 0, Message = "third task" }); |
| |
| timer.Change(0, 100); |
| } |
|
|
| |
| |
| |
| private void Timer_Tick(object state) |
| { |
| lock (this) |
| { |
| if (timerTaskQueue.Count == 0) |
| { |
| timer.Change(Timeout.Infinite, Timeout.Infinite); |
| return; |
| } |
| |
| TimerTask task = timerTaskQueue.Dequeue(); |
| |
| Thread.Sleep(task.WaitTime_msec); |
| Console.WriteLine(task.Message); |
| } |
| } |
| |
| } |
| |
| public class TimerTask |
| { |
| private int waitTime_msec; |
| |
| public int WaitTime_msec |
| { |
| get { return waitTime_msec; } |
| set { waitTime_msec = value; } |
| } |
| |
| private string message; |
| |
| public string Message |
| { |
| get { return message; } |
| set { message = value; } |
| } |
| } |
| |
| }
|