c# 线程委托结合

C# 多线程与委托示例
本文介绍了一个使用 C# 实现的多线程与委托的应用案例,包括如何定义委托、创建线程并在线程中调用委托方法。此外还展示了如何更新 UI 组件的内容,并确保在正确的线程上进行操作。

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"); }
        }
  
    }
}

转载于:https://www.cnblogs.com/skyado/archive/2009/10/30/1592764.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值