G2DTimer.cs 930 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using CommonLang;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Linq;
  6. using System.Text;
  7. namespace CommonFroms.G2D
  8. {
  9. public class G2DTimer : System.Windows.Forms.Timer
  10. {
  11. // 内部主线程命令 //
  12. private SyncMessageQueue<Action> mTasks = new SyncMessageQueue<Action>();
  13. public G2DTimer() { }
  14. public G2DTimer(IContainer container) : base(container) { }
  15. /// <summary>
  16. /// 保证在Task内部执行的代码线程安全
  17. /// </summary>
  18. /// <param name="task"></param>
  19. public void QueueTask(Action task)
  20. {
  21. mTasks.Enqueue(task);
  22. }
  23. private void do_task(Action task)
  24. {
  25. task.Invoke();
  26. }
  27. protected override void OnTick(EventArgs e)
  28. {
  29. base.OnTick(e);
  30. mTasks.ProcessMessages(do_task);
  31. }
  32. }
  33. }