zoukankan      html  css  js  c++  java
  • 揭秘委托与多线程使用

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

    namespace WindowsFormsApplication1
    {
        public partial class MainForm : Form
        {
            private delegate void ChangeTemperatureHander();
            public MainForm()
            {
                InitializeComponent();
            }

            private void toolStripMenuItem1_Click(object sender, EventArgs e)
            {

            }

            private void btnStart_Click(object sender, EventArgs e)
            {
                th = new Thread(this.Heating)

               //通过这个来说明该线程是主线程的子线程,当主线程关闭时,子线程一同关闭。

                th.IsBackground = true;
                th.Start();
            }

            private Thread th;
            private int temperature;
            private void Heating()
            {
                for (int i = 0; i < 100; i++)
                {
                    temperature = i;

                    //不能引用一个子线程来改变另外的一个线程的数据,故使用委托。
                    this.Invoke(new ChangeTemperatureHander(OnChangeTemparature));
                    Thread.Sleep(1000);
                }
            }
            private void OnChangeTemparature()
            {
                this.txtemperature.Text = temperature.ToString();
            }
        }
    }

  • 相关阅读:
    Net设计模式实例之简单工厂模式(Simple Factory Pattern)
    Net设计模式实例系列文章总结
    2019年工作总结
    在Asp.Net Core中集成Kafka(中)
    如何将生产环境的服务Docker镜像拉取到本地进行调试
    记一次EFCore类型转换错误及解决方案
    Asp.Net Core中创建多DbContext并迁移到数据库
    ABP中的AutoMapper
    EFCore中的导航属性
    Asp.Net Core 调用第三方Open API查询物流数据
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050685.html
Copyright © 2011-2022 走看看