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

  • 相关阅读:
    3.30作业
    3.30课堂
    3.29作业
    3,29课堂
    3。26作业
    3.26课堂
    3.25作业
    3.25课堂
    55、DOM与BOM的操作及事件的简介
    54、js的数据类型及对象
  • 原文地址:https://www.cnblogs.com/wangyhua/p/4050685.html
Copyright © 2011-2022 走看看