code_junkie

Can I create a thread that can modify the user interface, and that I can abort?

Nov 12th, 2011
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. private delegate void DoUIStuffDelegate(Thing1 arg1, Thing2 arg2);
  2. private void doUIStuff(Thing1 arg1, Thing2 arg2)
  3. {
  4. control1.Visible = false;
  5. this.Controls.Add(arg1.ToButton());
  6. ...
  7. control100.Text = arg2.ToString();
  8. }
  9.  
  10. ...
  11.  
  12. private void backgroundThread()
  13. {
  14. Thing1 arg1 = new Thing1();
  15. Thing2 arg2 = new Thing2();
  16.  
  17. this.Invoke(new DoUIStuffDelegate(doUIStuff), arg1, arg2);
  18. }
  19.  
  20. Thread uiStuffThread = null;
  21.  
  22. public void OnEventFired()
  23. {
  24. if (uiStuffThread != null)
  25. uiStuffThread.Abort();
  26.  
  27. uiStuffThread = new Thread(backgroundThread);
  28. uiStuffThread.Start();
  29. }
  30.  
  31. private delegate void DoUIStuffLine1Delegate();
  32. private delegate void DoUIStuffLine2Delegate(Thing1 arg1);
  33. ...
  34.  
  35. private delegate void DoUIStuffLine100Delegate(Thing2 arg2);
  36.  
  37. private void doUIStuffLine1()
  38. {
  39. control1.Visible = false;
  40. }
  41.  
  42. private void doUIStuffLine2()
  43. {
  44. this.Controls.Add(arg1.ToButton());
  45. }
  46.  
  47. ...
  48.  
  49. private void doUIStuffLine100(Thing2 arg2)
  50. {
  51. control100.Text = arg2.ToString();
  52. }
  53.  
  54. ...
  55.  
  56. private void backgroundThread()
  57. {
  58. Thing1 arg1 = new Thing1();
  59. Thing2 arg2 = new Thing2();
  60.  
  61. this.Invoke(new DoUIStuffLine1Delegate(doUIStuffLine1));
  62. this.Invoke(new DoUIStuffLine2Delegate(doUIStuffLine2), arg1);
  63. ...
  64. this.Invoke(new DoUIStuffLine100Delegate(doUIStuffLine100), arg2);
  65. }
  66.  
  67. Thread uiStuffThread = null;
  68.  
  69. public void OnEventFired()
  70. {
  71. if (uiStuffThread != null)
  72. uiStuffThread.Abort();
  73.  
  74. uiStuffThread = new Thread(backgroundThread);
  75. uiStuffThread.Start();
  76. }
  77.  
  78. private void doUIStuff()
  79. {
  80. Thing1 arg1 = new Thing1();
  81. Thing2 arg2 = new Thing2();
  82.  
  83. control1.Visible = false;
  84. this.Controls.Add(arg1.ToButton());
  85. ...
  86. control100.Text = arg2.ToString();
  87. }
  88.  
  89. Thread uiStuffThread = null;
  90.  
  91. public void OnEventFired()
  92. {
  93. if (uiStuffThread != null)
  94. uiStuffThread.Abort();
  95.  
  96. uiStuffThread = this.GetNiceThread(doUIStuff);
  97. uiStuffThread.Start();
  98. }
  99.  
  100. void worker_DoWork(object sender, DoWorkEventArgs e)
  101. {
  102. try {
  103. Action<Action> update = thingToDo =>
  104. {
  105. if (worker.CancellationPending) throw new SomeException();
  106. this.Invoke(thingToDo);
  107. };
  108.  
  109. //...
  110. string tmp = "abc"; // long running
  111. update(() => this.Text = tmp);
  112.  
  113. tmp = "def"; // long running
  114. update(() => textbox1.Text = tmp);
  115. } catch (SomeException) {
  116. e.Cancel = true;
  117. }
  118. }
  119.  
  120. private Thread workerThread;
  121.  
  122. private AsyncOperation operation;
  123.  
  124. public event EventHandler SomethingHappened;
  125.  
  126. public MySynchronizedClass()
  127. {
  128. operation = AsyncOperationManager.CreateOperation(null);
  129.  
  130. workerThread = new Thread(new ThreadStart(DoWork));
  131.  
  132. workerThread.Start();
  133. }
  134.  
  135. private void DoWork()
  136. {
  137. operation.Post(new SendOrPostCallback(delegate(object state)
  138. {
  139. EventHandler handler = SomethingHappened;
  140.  
  141. if(handler != null)
  142. {
  143. handler(this, EventArgs.Empty);
  144. }
  145. }), null);
  146.  
  147. operation.OperationCompleted();
  148. }
  149.  
  150. bw = new BackgroundWorkerExtended();
  151. bw.DoWork += (DoWorkEventHandler)work;
  152. bw.WorkerSupportsCancellation = true;
  153. //bw.WorkerReportsProgress = true;
  154. bw.RunWorkerCompleted += (RunWorkerCompletedEventHandler)workCompleted;
  155. //bw.ProgressChanged+=new ProgressChangedEventHandler(bw_ProgressChanged);
  156. bw.RunWorkerAsync();
Add Comment
Please, Sign In to add comment