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(),设置区域重绘
C#子线程中更新主线程UI-----注意项的更多相关文章
- runOnUiThread在子进程中更新主进程UI
package com.pingyijinren.test; import android.support.v7.app.AppCompatActivity; import android.os.Bu ...
- Android在子线程中更新UI(一)
MainActivity如下: package cc.testui1; import android.os.Bundle; import android.os.Handler; import andr ...
- android 不能在子线程中更新ui的讨论和分析
问题描写叙述 做过android开发基本都遇见过 ViewRootImpl$CalledFromWrongThreadException,上网一查,得到结果基本都是仅仅能在主线程中更改 ui.子线程要 ...
- C#子线程中更新ui
本文实例总结了C#子线程更新UI控件的方法,对于桌面应用程序设计的UI界面控制来说非常有实用价值.分享给大家供大家参考之用.具体分析如下: 一般在winform C/S程序中经常会在子线程中更新控件的 ...
- 使用Handler在子线程中更新UI
Android规定仅仅能在主线程中更新UI.假设在子线程中更新UI 的话会提演示样例如以下错误:Only the original thread that created a view hierach ...
- 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 ...
- Android多线程之(一)View.post()源码分析——在子线程中更新UI
提起View.post(),相信不少童鞋一点都不陌生,它用得最多的有两个功能,使用简便而且实用: 1)在子线程中更新UI.从子线程中切换到主线程更新UI,不需要额外new一个Handler实例来实现. ...
- 如何在子线程中更新UI
一:报错情况 android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that creat ...
- Android在子线程中更新UI(二)
MainActivity如下: package cc.testui2; import android.os.Bundle; import android.view.View; import andro ...
随机推荐
- byte数组转float实现与byte转换其它类型时进行&运算原理
下面是将byte数组转换为float的实现 public static float getFloat(byte[] b) { int accum = 0; accum = accum|(b[0] &a ...
- BZOJ3262:陌上花开 & 洛谷3810:三维偏序——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3262 https://www.luogu.org/problemnew/show/3810 Desc ...
- Linux系统启动详解(一)
本篇主要以Centos为例,讲述整个Linux系统启动过程,包括了grub引导,initramfs流程,/sbin/init执行rc.sysinit及rc的大体流程. 另外,本篇有一个实例来说明,将整 ...
- git 列出两个 commit 之间变更的文件列表
git diff <commit1> <commit2> --stat 如: git diff 74ecf17dc 1ee25ed3c --stat src/assets 上面 ...
- Android缓存
一个利用内存缓存和磁盘缓存图片的例子 public class BitmapCache { public static final String TAG = "debug"; pr ...
- poj 1743 后缀数组 最长不重叠子串
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 30941 Accepted: 10336 D ...
- qq快速登陆
http://www.cnblogs.com/1996V/p/7481823.html qq快速登陆
- HTML标签marquee 来制作页面滚动
页面的自动滚动效果,可由javascript来实现,但是今天无意中发现了一个html标签 - <marquee></marquee>可以实现多种滚动效果,无需js控制. 使用m ...
- 对WebView进行的一些设置
webView.getSettings().setJavaScriptEnabled(true); //使用setting WebSettings webSettings = webView.getS ...
- 【BZOJ】2120: 数颜色 带修改的莫队算法
[题意]给定n个数字,m次操作,每次询问区间不同数字的个数,或修改某个位置的数字.n,m<=10^4,ai<=10^6. [算法]带修改的莫队算法 [题解]对于询问(x,y,t),其中t是 ...