- 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:
- ====================================== Work. cs ==================================
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Text;
- Using System. Threading;
- Using System. ComponentModel;
-
- Namespace ThreadTest
- {
- Public class Work
- {
- Thread thd;
- Private static int count = 0;
-
- Public Work ()
- {
- Thd = new Thread (new ThreadStart (thdDoWork ));
- Thd. Name = "NewThread ";
- }
-
- WarningForm wf; // The prompt form.
- Private void thdDoWork () // background work
- {
- While (true)
- {
- If (count ++ % 10) = 0)
- {
- Wf = new WarningForm (); // create and display the prompt form
- Wf. Show (); // display the form as a non-mode dialog box
- }
- Thread. Sleep (600 );
- }
- }
-
- Public void ThdStart ()
- {
- Thd. Start ();
- }
- }
- }
- ====================================== Work. cs ==================================
- In addition, add the following in the constructor of WarningForm:
- Console. WriteLine ("WarningForm Created in:" + Thread. CurrentThread. Name );
- In this way, we can clearly see which thread WarningForm is created and displayed.
- 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.
-
- Change "wf. Show ()" to "wf. ShowDialog ()" and try again:
- Debug: in the pop-up window, 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.
-
- 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?
- 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!
- We modify Work. cs as follows:
- ====================================== Work. cs ==================================
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Text;
- Using System. Threading;
- Using System. ComponentModel;
-
- Namespace ThreadTest
- {
- Public class Work
- {
- Public BackgroundWorker bg;
- Private static int count = 0;
-
- Public Work ()
- {
- Bg = new BackgroundWorker ();
- }
-
- Private void bgDoWork (object sender, DoWorkEventArgs e)
- {
- Thread. CurrentThread. Name = "BgWorker"; // modify the Name of the background Thread
- While (true)
- {
- If (count ++ % 10) = 0)
- Bg. ReportProgress (count); // activation event
- Thread. Sleep (500 );
- }
- }
-
- Public void BgStart ()
- {
- Bg. WorkerReportsProgress = true;
- Bg. DoWork + = new DoWorkEventHandler (bgDoWork );
- Bg. RunWorkerAsync ();
- }
-
-
- }
- }
- ====================================== Work. cs ==================================
- At the same time, write the following in Form1.cs:
- ===================================== Form1.cs fragment ====================== ====================
- WarningForm wf;
- Public Form1 ()
- {
- InitializeComponent ();
- Thread. CurrentThread. Name = "MainThread"; // modify the Name of the main Thread
- }
- Private void button3_Click (object sender, EventArgs e)
- {
- // Work is the Work object
- Work. bg. ProgressChanged + = new ProgressChangedEventHandler (bg_ProgressChanged); // register this event
- Work. BgStart ();
- }
-
- Private void bg_ProgressChanged (object sender, ProgressChangedEventArgs e)
- {
- // A prompt form is displayed when an event occurs.
- Wf = new WarningForm ();
- Wf. Show ();
- // Wf. ShowDialog ();
- }
- ===================================== Form1.cs fragment ====================== ====================
- 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!
-
- 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, please be sure to keep this source http://pzy4447.blog.51cto.com/2004939/699273