code_junkie

Using threads to count the loops in C# events

Nov 13th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. void IncrPaintQueue()
  2. {
  3. PaintQueue++;
  4. Thread.Sleep(100);
  5. }
  6.  
  7. int PaintQueue = 0;
  8.  
  9. private void SegmentList_SelectedIndexChanged(object sender, EventArgs e)
  10. {
  11. // We need to know how many threads this may possibly spawn.
  12. int MyQueue = PaintQueue;
  13.  
  14. // Start a thread to increment the counter.
  15. Thread Th = new Thread(IncrPaintQueue);
  16. Th.IsBackground = true;
  17. Th.Start();
  18. Th.Join();
  19.  
  20. // if I'm not the last thread, then just exit.
  21. // The last thread will do the right calculations.
  22. if (MyQueue != PaintQueue - 1)
  23. return;
  24.  
  25. // Reset the PaintQueue counter.
  26. PaintQueue = 0;
  27.  
  28. // ... do fancy calculations here...
  29. }
  30.  
  31. public class SelectionEndListView : ListView
  32. {
  33. private System.Windows.Forms.Timer m_timer;
  34. private const int SELECTION_DELAY = 50;
  35.  
  36. public SelectionEndListView()
  37. {
  38. m_timer = new Timer();
  39. m_timer.Interval = SELECTION_DELAY;
  40. m_timer.Tick += new EventHandler(m_timer_Tick);
  41. }
  42.  
  43. protected override void OnSelectedIndexChanged(EventArgs e)
  44. {
  45. base.OnSelectedIndexChanged(e);
  46.  
  47. // restart delay timer
  48. m_timer.Stop();
  49. m_timer.Start();
  50. }
  51.  
  52. private void m_timer_Tick(object sender, EventArgs e)
  53. {
  54. m_timer.Stop();
  55.  
  56. // Perform selection end logic.
  57. Console.WriteLine("Selection Has Ended");
  58. }
  59. }
  60.  
  61. private void myListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  62. {
  63. if (e.IsSelected)
  64. {
  65. // do your logic here
  66. }
  67. }
Add Comment
Please, Sign In to add comment