zoukankan      html  css  js  c++  java
  • 更新主窗口控件的内容1:子线程工作时同时更新主线程内的控件内容

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Windows.Forms;
     9 using System.Threading;
    10 
    11 namespace WindowsFormsApplication1
    12 {
    13     public partial class MainForm : Form
    14     {
    15         public MainForm()
    16         {
    17             InitializeComponent();
    18         }
    19 
    20         delegate void SetMsg(string msg);
    21         //private void SetMsgInfo(string msg)
    22         //{
    23         //    this.textBox1.Text = msg; 
    24         //}
    25 
    26         private void button1_Click(object sender, EventArgs e)
    27         {          
    28             Thread thread = new Thread(M1);
    29             thread.Start();
    30         }
    31 
    32         void M1()
    33         {
    34             for (int i = 0; i < 10; i++)
    35             {
    36                 //this.Invoke(new SetMsg(SetMsgInfo), new object[] { i.ToString() });
    37                 this.Invoke(new SetMsg((o) => { this.textBox1.Text = o; }), new object[] { i.ToString() });//匿名方法不懂的可以去查一下。
    38                 Thread.Sleep(500);
    39             }
    40         }
    41     }
    42 }

    =================================================================================
    这段代码如果使用以后的代码去替换,也可以达到每秒变换this.label1.Text的目的,但是这个用户体验却很差:在变化值时,窗体不能移动。用上面的方法可以避开这个问题。
            private void button1_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < 10; i++)
                {
                    this.label1.Text = i.ToString();
                    this.Update();
                    Thread.Sleep(1000);
                }
            }
     
  • 相关阅读:
    perl 分析binlog
    perl 替换
    perl s s* s+
    17.3.2 Using Replication with Different Master and Slave Storage Engines
    17.3.1.1 Backing Up a Slave Using mysqldump
    MySQL时间戳和日期相互转化
    MySQL中ROUND和TRUNCATE的区别
    17.3 Replication Solutions
    17.2.2.2 Slave Status Logs
    relay-log命名规则
  • 原文地址:https://www.cnblogs.com/pnljs/p/3622958.html
Copyright © 2011-2022 走看看