zoukankan      html  css  js  c++  java
  • C#子线程中更新主线程UI-----注意项

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

    namespace WindowsFormsAppTestDoEvent
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 10000; i++)
    {
    label1.Text = i.ToString();
    Application.DoEvents();
    //测试了一下,没有Application.DoEvents()的时候,Label基本处于假死机状态,最后显示一个9999,
    //加上后会数字变换正常显示。
    //从这个测验后DoEvents的功能,应该DoEvents就好比实现了进程的同步。在不加的时候,因为优先级的问题,程序会执行主进程的代码,再执行别代码,而加了以后就可以同步执行
    }
    }
    }
    }

    主线程UI更新不及时或者多线程时更新不正确,可以使用一下方式解决

    1、Application.DoEvents();

    测试了一下,没有Application.DoEvents()的时候,Label基本处于假死机状态,最后显示一个9999,
    //加上后会数字变换正常显示。
    //从这个测验后DoEvents的功能,应该DoEvents就好比实现了进程的同步。在不加的时候,因为优先级的问题,程序会执行主进程的代码,再执行别代码,而加了以后就可以同步执行

    2、使用线程等待,即Thread.Sleep(5000);

    3、使用winForm的invalidate(),设置区域重绘

  • 相关阅读:
    利用windows 127.0.0.1:30000 代理在linux下工作
    nginx与ssh 把内网转为公网开发服务器
    php errorlog 记录
    dockerfile php 开发
    ubuntu
    k8s 1.9.1 centos7 install 离线安装
    kubernetes
    linux字符设备驱动--基本知识介绍
    linux应用程序设计--Makefile工程管理
    linux应用程序设计--GDB调试
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/8350130.html
Copyright © 2011-2022 走看看