C # Two security methods for multi-threaded controls (delegate mode and BackgroundWorker Control Mode)

Source: Internet
Author: User

The implemented function is

1. After pressing button2, label2 displays a number every 100 milliseconds, ranging from 0 to 99. (Delegated implementation)

2. After pressing button3, label3 is displayed as the current time five seconds after the simulated time-consuming operation. (Implemented in BackgroundWorker Mode)

3. When executed in step 1 and step 2, you can press button1 to change the content of label1 to the content of textbox1. (The main thread is controlled here to display the multi-thread master thread without deadlock)

 

 

 1 using System;
2 using System. Collections. Generic;
3 using System. ComponentModel;
4 using System. Data;
5 using System. Drawing;
6 using System. Linq;
7 using System. Text;
8 using System. Windows. Forms;
9 using System. Threading;
10
11 namespace WindowsFormsApplication1
12 {
13 public partial class Form1: Form
14 {
15 private delegate void SetState (int x); // proxy
16 public Form1 ()
17 {
18 InitializeComponent ();
19 Thread. CurrentThread. Name = "Main"; // master Thread Name
20 Console. WriteLine (Thread. CurrentThread. Name );
21}
22
23 private void TempFunction () // intermediate function, used to call the target function multiple times in a row and simulate latency
24 {
25 for (int I = 0; I <100; I ++)
26 {
27 ThreadFunction (I );
28 Thread. Sleep (100 );
29}
30}
31 private void ThreadFunction (int x) // actual control operation function
32 {
33 if (label2.InvokeRequired) // use a delegate to operate
34 {
35 SetState ss = new SetState (ThreadFunction );
36 Invoke (ss, new object [] {x });
37 Console. WriteLine (Thread. CurrentThread. Name );
38}
39 else // normal operation
40 {
41 label2.Text = x. ToString ();
42 label2.Update ();
43 Console. WriteLine (Thread. CurrentThread. Name );
44}
45
46}
47
48 private void button#click (object sender, EventArgs e) // The button operation of the main thread to display the difference
49 {
50 label1.Text = textBox1.Text;
51}
52
53 private void button2_Click (object sender, EventArgs e) // press the button to start Multithreading
54 {
55 Thread th1 = new Thread (new ThreadStart (TempFunction ));
56 th1.IsBackground = true;
57 th1.Name = "kidfruit ";
58 th1.Start ();
59}
60
61 private void backgroundworker=dowork (object sender, DoWorkEventArgs e) // use the BackgroundWorker Control
62 {
63 Thread. Sleep (5000); // simulate operation Delay
64}
65
66 private void backgroundworkerappsrunworkercompleted (object sender, RunWorkerCompletedEventArgs e) // after the time-consuming operation of BackgroundWorker is completed
67 {
68 label3.Text = DateTime. Now. ToLongTimeString ();
69}
70
71 private void button3_Click (object sender, EventArgs e) // start time-consuming operations of BackgroundWorker
72 {
73 backgroundWorker1.RunWorkerAsync ();
74}
75
76
77}
78}
79

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.