现在的位置: 首页 > 综合 > 正文

C# 事件

2013年11月07日 ⁄ 综合 ⁄ 共 705字 ⁄ 字号 评论关闭
  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Test test = new Test();
            test.OnEventName += new AbcEventHandler(SetTip);
            new System.Threading.Thread(() =>
            {
                test.xf();
            }).Start();
        }

        public delegate void SetLalel(string v);

        void SetTip(string v)
        {
            if (label1.InvokeRequired)
            {
                SetLalel slb = new SetLalel(SetTip);
                label1.BeginInvoke(slb, new Object[] { v });
            }
            else
                label1.Text = v;
        }
    }

namespace aaa{ public delegate void AbcEventHandler(string s); public class Test { public event AbcEventHandler OnEventName; public void EventName(string ss) { OnEventName(ss); } /// <summary> /// /// </summary> public void xf() { for (int i = 0; i < 50; i++)
{ OnEventName(i.ToString());//更新 UI 显示  System.Threading.Thread.Sleep(2000); } } }}


抱歉!评论已关闭.