zoukankan      html  css  js  c++  java
  • C# 多线程 与 委托

    简单的多线程:

            protected void Page_Load(object sender, EventArgs e)
            {
                ParameterizedThreadStart _ParameterizedThreadStart = new ParameterizedThreadStart(MyMethod);
                Thread t = new Thread(_ParameterizedThreadStart);
                object o = "sdfsf";
                t.Start(o);
            }
    
    
            private void MyMethod(object o)
            {
                //执行任务
    }

    使用委托的多线程:

    public partial class Demo : Form
        {
            public Demo()
            {
                InitializeComponent();
            }
    
            //定义委托
            delegate void MyDelegate(string str);
            //声明委托
            MyDelegate _MyDelegate;
            //声明线程
            private Thread t;
            private void Demo_Load(object sender, EventArgs e)
            {
                //给函数指定委托
                _MyDelegate = new MyDelegate(SetLblTxt);
            }
    
            //执行委托
            private void Run()
            {
                while (true)
                {
                    lblShow.Invoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() });
                    Thread.Sleep(100);
                }
            }
    
            //设定lable的text
            private void SetLblTxt(string o)
            {
                this.lblShow.Text = o.ToString();
            }
    
            private void btnBegin_Click(object sender, EventArgs e)
            {
                t = new Thread(new ThreadStart(Run));
                t.Start();
            }
    
            private void Demo_FormClosing(object sender, FormClosingEventArgs e)
            {
                //终止线程
                if (t.IsAlive) 
                {
                    t.Abort(); 
                }
            }
        }

     有返回值的多线程:

    //定义委托
            delegate string MyDelegate(string str);
            //声明委托
            MyDelegate _MyDelegate;
            //声明线程
            private Thread t;
            private void Demo_Load(object sender, EventArgs e)
            {
                //给函数指定委托
                _MyDelegate = new MyDelegate(SetLblTxt);
            }
    
            //执行委托
            private void Run()
            {
                object o = new object();
                while (true)
                {
                    lock (lblShow)
                    {
                        //lblShow.Invoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() }); //其实是同步调用
                        IAsyncResult result = lblShow.BeginInvoke(_MyDelegate, new object[] { Guid.NewGuid().ToString() }); //异步调用
                        MessageBox.Show(lblShow.EndInvoke(result).ToString());  //获得返回值
                        Thread.Sleep(1000);
                    }
                }
            }
    
            //设定lable的text
            private string SetLblTxt(string o)
            {
                this.lblShow.Text = o.ToString();
                return o.ToString();
            }
    
            private void btnBegin_Click(object sender, EventArgs e)
            {
                t = new Thread(new ThreadStart(Run));
                t.Start();
            }
    
            private void Demo_FormClosing(object sender, FormClosingEventArgs e)
            {
                //终止线程
                if (t == null) return;
                if (t.IsAlive) 
                {
                    t.Abort(); 
                }
            }
  • 相关阅读:
    nginx显示中文乱码
    Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details
    linux 7 网卡配置
    zabbix 离线安装
    linux alias 设置命令别名
    重启redis shell脚本
    docker离线安装
    ansible 批量添加用户
    linux 7 离线安装ansible
    Linux 7 配置163yum源
  • 原文地址:https://www.cnblogs.com/wugang/p/2934632.html
Copyright © 2011-2022 走看看