这是我在面试过程中遇到的一个小测试,很可惜,当天未能圆满完成,虽然第二天经过实际测试已经OK 了,但学无止境,继续努力吧,特将此代码贴在这里,供以后学习使用;
1 namespace mytest 2 { 3 public partial class Form1 : Form 4 { 5 public Form1() 6 { 7 InitializeComponent(); 8 } 9 10 private void button1_Click(object sender, EventArgs e) 11 { 12 timer1.Interval = 200; 13 timer1.Enabled = true; 14 15 } 16 17 public delegate void mydelegate( ); 18 public mydelegate myway; 19 public mydelegate myway2; 20 private void check(object obj) 21 { 22 myway(); 23 24 } 25 private void check2(object obj) 26 { 27 myway2(); 28 29 } 30 private void add1( ) 31 { 32 33 int i = Convert.ToInt32(label1.Text); 34 i++; 35 if (!this.IsHandleCreated || this.IsDisposed) return; 36 if (InvokeRequired) 37 { 38 this.Invoke((MethodInvoker)delegate 39 { 40 41 label1.Text = i.ToString(); 42 43 }); 44 } 45 else 46 { 47 48 } 49 50 } 51 52 private void add2() 53 { 54 55 int i = Convert.ToInt32(label2.Text); 56 i++; 57 if (!this.IsHandleCreated || this.IsDisposed) return; 58 if (InvokeRequired) 59 { 60 this.Invoke((MethodInvoker)delegate 61 { 62 63 label2.Text = i.ToString(); 64 65 int j = Convert.ToInt32(label1.Text); 66 j = j + i; 67 label3.Text = j.ToString(); 68 69 }); 70 } 71 else 72 { 73 74 } 75 76 } 77 78 79 80 81 private void Form1_Load(object sender, EventArgs e) 82 { 83 myway += new mydelegate(add1); 84 myway2 += new mydelegate(add2); 85 } 86 87 88 89 private void timer1_Tick(object sender, EventArgs e) 90 { 91 92 93 94 ThreadPool.QueueUserWorkItem(new WaitCallback(check), null);//执行线程池 95 ThreadPool.QueueUserWorkItem(new WaitCallback(check2), null);//执行线程池 96 } 97 } 98 }
窗体界面如下:
不积跬步无以至千里