c# 线程委托结合

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ComDevices
{
    public partial class Form2 : Form
    {
        public delegate void MyEventHandler();
        public delegate void MyEventHandler1(int m);
        MyEventHandler ClickHandle;
        MyEventHandler AccessTextBox;
        MyEventHandler1 AccessTextBox1;
        Thread th;
        public Form2()
        {
            InitializeComponent();
            this.Load+=new EventHandler(Form2_Load);
            this.FormClosed+=new FormClosedEventHandler(Form2_FormClosed);
            ClickHandle=new MyEventHandler(button_Click);
            AccessTextBox = new MyEventHandler(button_Click);
            AccessTextBox1 = new MyEventHandler1(button_Click1);
            //th = new Thread(delegate(){ button_Click1(1); });
            th = new Thread(delegate() { AccessTextBox1(1); });
            th.IsBackground = true;
            try
            {
             
                th.Start();
            }
            catch
            {
                MessageBox.Show("Thread Error");
            }
      
        }
        protected void Form2_FormClosed(object a, EventArgs b)
        { th.Abort();
        Dispose();
    }
        protected void  Form2_Load(object a,EventArgs b)
        {
            //try
            //{
            //    Form f = (Form)a;
            //    MessageBox.Show(f.Name);
            //}
            //catch
            //{ }
        }

        private void Form2_KeyDown(object sender, KeyEventArgs e)
        {
           
            textBox1.AppendText(e.KeyValue+" ");
        }
    
        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            textBox1.AppendText(" "+e.KeyValue + ":");
        }

        public  void button1_Click(object sender, EventArgs e)
        {
            try { string a=DateTime.Now.ToString("yyyy-m-dd-hh:mm:ss"); MessageBox.Show(a.ToString()); }
            catch { }
            if(MessageBox.Show("content", "title", MessageBoxButtons.OKCancel)==DialogResult.OK)
                MessageBox.Show("ok la"); ;
            if (MessageBox.Show("content", "title", MessageBoxButtons.YesNo) == DialogResult.Yes)
                MessageBox.Show("yes la"); ;
        }
        public void button_Click1(int m)
        {
            if (this.textBox1.InvokeRequired)
                this.textBox1.Invoke(AccessTextBox1, m);
            else
                this.textBox1.AppendText("aaaaa//r//naaaaa");
        }
        public void button_Click()
        {
            if (this.textBox1.InvokeRequired)
                this.textBox1.Invoke(AccessTextBox);
            else
            this.textBox1.AppendText("aaaaa//r//naaaaa");
            /*
            try { string a = DateTime.Now.ToString("yyyy-m-dd-hh:mm:ss"); MessageBox.Show(a.ToString()); }
            catch { }
            if (MessageBox.Show("content", "title", MessageBoxButtons.OKCancel) == DialogResult.OK)
                MessageBox.Show("ok la"); ;
            if (MessageBox.Show("content", "title", MessageBoxButtons.YesNo) == DialogResult.Yes)
                MessageBox.Show("yes la"); ;*/
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                this.Invoke(ClickHandle, null);
            }
            catch
            { MessageBox.Show("Handler Error"); }
        }
  
    }
}

posted @ 2009-10-30 09:31  会游泳dě鱼  阅读(232)  评论(0编辑  收藏  举报