1. requirement: the background continuously performs a certain kind of work. A prompt is displayed when a specific result is obtained, and the work continues. My first response is that the job should be executed in the background thread, and the prompt form should be created/displayed when the condition is met. The Code is as follows:
2. ====================================== Work. cs ==================================
3. using System;
4. using System. Collections. Generic;
5. using System. Linq;
6. using System. Text;
7. using System. Threading;
8. using System. ComponentModel;
9.
10. namespace ThreadTest
11 .{
12. public class Work
13 .{
14. Thread thd;
15. private static int count = 0;
16.
17. public Work ()
18 .{
19. thd = new Thread (new ThreadStart (thdDoWork ));
20. thd. Name = "NewThread ";
21 .}
22.
23. WarningForm wf; // prompt form
24. private void thdDoWork () // background work
25 .{
26. while (true)
27 .{
28. if (count ++ % 10) = 0)
29 .{
30. wf = new WarningForm (); // create and display the prompt form
31. wf. Show (); // display the form as a non-mode dialog box
32 .}
33. Thread. Sleep (600 );
34 .}
35 .}
36.
37. public void ThdStart ()
38 .{
39. thd. Start ();
40 .}
41 .}
42 .}
43. ====================================== Work. cs ==================================
44. At the same time, add the following to the constructor of WarningForm:
45. Console. WriteLine ("WarningForm Created in:" + Thread. CurrentThread. Name );
46. In this way, we can clearly see which thread WarningForm is created and displayed.
47. Debug: the prompt window is displayed, and the background work is not stopped, but the prompt window does not respond. The background outputs "WarningForm Created in: NewThread ". This is because the prompt form is displayed in non-mode, while the background thread displays the form and continues to execute it. It does not maintain the response to the prompt form interface, so the above phenomenon occurs.
48.
49. Change "wf. Show ()" to "wf. ShowDialog ()" and try again:
50. Debug: the prompt window is displayed. The interface does not die. The background outputs "WarningForm Created in: NewThread", but the background work stops. This is because ShowDialog () shows the form as a mode, that is, the display of the form blocks the running of the current thread.
51.
52. In this case, it does not work to display the prompt form in the background thread. How can I capture this message in the main thread when the background thread needs to display the form?
53. Ha, this guest is lucky. The ReportProgress (int percentProgress, object userState) method of BackgroundWorker is most suitable for solving this problem. We can register the BackgroundWorker ProgressChanged event in the main thread. When the BackgroundWorker object calls ReportProgress (), the method registered with the ProgressChanged event will be executed in the main thread, it has no effect on the running of background threads, and then prompts whether the form is displayed as a mode or not!
54. Modify Work. cs as follows:
55. ====================================== Work. cs ==================================
56. using System;
57. using System. Collections. Generic;
58. using System. Linq;
59. using System. Text;
60. using System. Threading;
61. using System. ComponentModel;
62.
63. namespace ThreadTest
64 .{
65. public class Work
66 .{
67. public BackgroundWorker bg;
68. private static int count = 0;
69.
70. public Work ()
71 .{
72. bg = new BackgroundWorker ();
73 .}
74.
75. private void bgDoWork (object sender, DoWorkEventArgs e)
76 .{
77. Thread. CurrentThread. Name = "BgWorker"; // modify the Name of the background Thread.
78. while (true)
79 .{
80. if (count ++ % 10) = 0)
81. bg. ReportProgress (count); // activate the event
82. Thread. Sleep (500 );
83 .}
84 .}
85.
86. public void BgStart ()
87 .{
88. bg. WorkerReportsProgress = true;
89. bg. DoWork + = new DoWorkEventHandler (bgDoWork );
90. bg. RunWorkerAsync ();
91 .}
92.
93.
94 .}
95 .}
96. ====================================== Work. cs ==================================
97. At the same time, write the following in Form1.cs:
98. ===================================== Form1.cs fragment ====================== ====================
99. WarningForm wf;
100. public Form1 ()
101 .{
102. InitializeComponent ();
103. Thread. CurrentThread. Name = "MainThread"; // modify the Name of the main Thread
104 .}
105. private void button3_Click (object sender, EventArgs e)
106 .{
107. // work is the Work object
108. work. bg. ProgressChanged + = new ProgressChangedEventHandler (bg_ProgressChanged); // register this event
109. work. BgStart ();
110 .}
111.
112. private void bg_ProgressChanged (object sender, ProgressChangedEventArgs e)
113 .{
114. // The prompt form is displayed when an event occurs.
115. wf = new WarningForm ();
116. wf. Show ();
117. // wf. ShowDialog ();
118 .}
119. ===================================== Form1.cs fragment ====================== ====================
120. Debug, output "WarningForm Created in: MainThread", you can see that the form is Created and displayed in the main thread. No matter whether the prompt form is in the mode or not, everything can run normally. At this point, we have implemented the required functions haha!
121.
122. in addition, note ReportProgress (int percentProgress, object userState). The second parameter is of the object type, in addition, the features described above make it very convenient for BackgroundWorker to communicate with the interface, and we can use it when updating the control. If you use a Thread object, you need to use the delegate to call the control's Invoke or BeginInvoke method, which is much more troublesome. But I think the latter must have some unique advantages. I don't know yet. Please give me some advice ..
This article is from the "Little pang DE blog" blog