Advertisement
Guest User

Untitled

a guest
Sep 30th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. private void RefreshThread()
  2. {
  3. var watch = Stopwatch.StartNew();
  4. while (true)
  5. {
  6. UpdateAllNetworks();
  7. UpdateAllInterferences();
  8. UpdateAllColors();
  9.  
  10. switch (ActivePage)
  11. {
  12. case Page.Start:
  13. break;
  14. case Page.Networks:
  15. this.Invoke((MethodInvoker)delegate
  16. {
  17. UpdateDataGridWithNetworks();
  18. ClearGraphs();
  19. Draw24GHzGraph();
  20. DrawSignalsOverTimeGraph();
  21. });
  22. break;
  23. case Page.Channels:
  24. break;
  25. case Page.Analyze:
  26. break;
  27. default:
  28. break;
  29. }
  30. watch.Stop();
  31. int elapsedMs = (int) watch.ElapsedMilliseconds;
  32.  
  33. if (elapsedMs < Constants.NetworksRefreshThreadInterval)
  34. Thread.Sleep(Constants.NetworksRefreshThreadInterval - elapsedMs);
  35. }
  36. }
  37.  
  38. public class CustomDataGridView : DataGridView
  39. {
  40. ...
  41. protected override void OnCellClick(DataGridViewCellEventArgs e)
  42. {
  43. base.OnCellClick(e);
  44. int Index = e.RowIndex;
  45.  
  46. if (Index != -1)
  47. {
  48. DataGridViewRow row = Rows[Index];
  49. PrimaryKeyForSelectedRow = row.Cells[KeyName].Value.ToString();
  50. }
  51. }
  52. }
  53.  
  54. class Program
  55. {
  56. static void Main(string[] args)
  57. {
  58. var helper = new Looper(5000, YourMethod_RefreshThread);
  59. helper.Start();
  60. }
  61.  
  62. private static void YourMethod_RefreshThread()
  63. {
  64. Console.WriteLine(DateTime.Now);
  65. }
  66. }
  67.  
  68. public class Looper
  69. {
  70. private readonly Action _callback;
  71. private readonly int _interval;
  72.  
  73. public Looper(int interval, Action callback)
  74. {
  75. if(interval <=0)
  76. {
  77. throw new ArgumentOutOfRangeException("interval");
  78. }
  79. if(callback == null)
  80. {
  81. throw new ArgumentNullException("callback");
  82. }
  83. _interval = interval;
  84. _callback = callback;
  85. }
  86.  
  87. private void Work()
  88. {
  89. var next = Environment.TickCount;
  90.  
  91. do
  92. {
  93. if (Environment.TickCount >= next)
  94. {
  95. _callback();
  96. next = Environment.TickCount + _interval;
  97. }
  98. Thread.Sleep(_interval);
  99.  
  100. } while (IsRunning);
  101. }
  102.  
  103. public void Start()
  104. {
  105. if (IsRunning)
  106. {
  107. return;
  108. }
  109. var thread = new Thread(Work);
  110. thread.Start();
  111. IsRunning = true;
  112. }
  113.  
  114. public void Stop()
  115. {
  116. this.IsRunning = false;
  117. }
  118.  
  119. public bool IsRunning { get; private set; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement