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(),设置区域重绘

C#子线程中更新主线程UI-----注意项的更多相关文章

  1. runOnUiThread在子进程中更新主进程UI

    package com.pingyijinren.test; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...

  2. Android在子线程中更新UI(一)

    MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...

  3. android 不能在子线程中更新ui的讨论和分析

    问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...

  4. C#子线程中更新ui

    本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...

  5. 使用Handler在子线程中更新UI

    Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...

  6. iOS 报错:(子线程中更新UI)This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    今天在写程序的时候,使用Xcode 运行工程时报出下面的错误错信息,我还以为是什么呢,好久没遇到过这样的错误了. **ProjectName[1512:778965] This application ...

  7. Android多线程之(一)View.post()源码分析——在子线程中更新UI

    提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...

  8. 如何在子线程中更新UI

    一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...

  9. Android在子线程中更新UI(二)

    MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...

随机推荐

  1. HDU-3974 Assign the task题解报告【dfs序+线段树】

    There is a company that has N employees(numbered from 1 to N),every employee in the company has a im ...

  2. winform登录代码

    Program.cs文件中 static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThr ...

  3. mac命令行快捷键

    其实不想每次输入host和user,可以在 ~/.ssh/config文件写上配置alias信息,以后ssh的时候根据alias即可.如: Host alias-name HostName ip_ad ...

  4. java中Mysql开发

    [IntelliJ IDEA 12使用]导入外部包 http://www.cnblogs.com/haochuang/p/3491959.html JDBC导入包即可 http://blog.163. ...

  5. centos 前端环境搭建

    Node.js 安装 wget 下载安装 yum -y install gcc make gcc-c++ openssl-devel wget node v6.11.0 下载 wget https:/ ...

  6. oracle实用的sqlplus命令

    有时候难免没有工具,得自己手动输入sqlplus命令 执行SQL文件:@sql文件,例如:@/home/myuser/sql/test.sql查看数据库存在的存储过程:Select object_na ...

  7. CCD与CMOS的区别

    我们在购买相机或是摄像机时,都会看到使用CMOS镜头或是CCD镜头,那么CCD与CMOS是什么意思呢,CCD与CMOS的区别是什么?首先,让我们了解CCD与CMOS的意思. CCDCCD使用一种高感光 ...

  8. 应用maven自动部署的脚本

    @(编程) 最近写了一个自动部署的脚本,可以一键部署到测试服务器或者生产服务器上,包括一个函数脚本和一个调用脚本,比较简单,记录如下. 特点如下: 部署前自动备份 可以部署tomcat项目和java项 ...

  9. debian自动挂载ntfs硬盘

    首先下载安装ntfs-3g apt-get install ntfs-3g 然后查看分区信息 fdisk -l Device Boot Start End Blocks Id System /dev/ ...

  10. HDU 2608 底数优化分块 暴力

    T(n) as the sum of all numbers which are positive integers can divied n. and S(n) = T(1) + T(2) + T( ...