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

  • 相关阅读:
    WeX5那些坑
    项目总结-微信公众平台Html5
    项目总结-APP中的HTML5
    夜幕团队成员的工资究竟几 K ?
    Docker竟然还能这么玩?商业级4G代理搭建实战!
    今天,大佬云集的夜幕团队正式成立了!
    InnoDB物理行中null值的存储的推断与验证
    探究InnoDB数据页内部行的存储方式
    DAO模式
    JDBC
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050685.html
Copyright © 2011-2022 走看看