• 简单多线程+委托+事件


    今天写个个小程序.需要用到多线程.久了没有用,很模糊,现在花20分钟记录下.怕以后又忘记了.

    写的时候.变量命名就没有过多考虑了,代码挺简单.就不加注释了.

    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 MultThread_Dlg
    {
        public partial class Form1 : Form
        {
            delegate void DgNum(string num);
            DgNum dgnum = null;
    
    
            delegate void DlgNum(string num);
            event DlgNum eventdlgNum = null;
            public Form1()
            {
                InitializeComponent();
                dgnum = new DgNum(DoNum);
                eventdlgNum += new DlgNum(DoeventdlgNum);
                userControl11.dgClikeNode += new UserControl1.DGClikeNode(userControl11_dgClikeNode);//事件只能+=
            }
    
    
            #region "委托多线程"
            private void DoNum(Object str)
            {
                textBox1.Text = (string)str;
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Thread td = new Thread(GetNum);
                td.Start();
            }
            private void GetNum()
            {
                for (int i = 0; i < 100000; i++)
                {
                    this.Invoke(dgnum, i.ToString());
                }
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                System.Environment.Exit(-1);
            }
            #endregion
    
            #region "事件的使用"
            void DoeventdlgNum(object num)
            {
                textBox2.Text = num.ToString();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                Thread td = new Thread(EventNum);
                td.Start();
    
            }
    
            private void EventNum()
            {
                for (int i = 0; i < 100000; i++)
                {
                    this.Invoke(eventdlgNum, i.ToString());//
                }
            }
             
            void userControl11_dgClikeNode(string str)
            {
                MessageBox.Show(str);
            }
            #endregion
           
           
    
    
    
        }
    }

    用户自定义控件使用委托

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    
    namespace MultThread_Dlg
    {
        public partial class UserControl1 : UserControl
        {
            public delegate void  DGClikeNode(string str);
           public  event DGClikeNode dgClikeNode = null;
            public UserControl1()
            {
                InitializeComponent();
            }
    
            private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (dgClikeNode != null)
                {
                    dgClikeNode(e.Node.Text);
                }
            }
        }
    }

    附上源码链接地址:https://files.cnblogs.com/xyong/MultThread_Dlg.zip

    ps:多次点击new出的新线程也会阻塞主线程?


    作者:javaoraspx
    出处:http://www.cnblogs.com/xyong/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    我们参与投资36Kr股权众筹项目“易途8”的决策过程
    【R】Rstudio set up
    [Hadoop]安装
    [LAMP]安装-Debian
    【SVM】清晰明了的理论文章
    [MachineLearning]KNN
    Social Network 社交网络分析
    【Debian】install
    【Ubuntu】Windows硬盘安装Ubuntu14.04
    【R】均值假设检验
  • 原文地址:https://www.cnblogs.com/xyong/p/3061878.html
Copyright © 2020-2023  润新知