本文主要是介绍C#【中级篇】Winform中Application.DoEvents的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、Winform中控件布局如下:
二、源代码:
using System;
using System.Diagnostics;
using System.Windows.Forms;namespace WindowsFormsApp2
{public partial class Form1 : Form{public Form1(){InitializeComponent();}//使用DoEvents,耗时长,建议使用异步操作async/awaitprivate void button1_Click(object sender, EventArgs e){Stopwatch sp = new Stopwatch();sp.Start();for (int i = 0; i < 10000; i++){textBox1.Text = i.ToString();Application.DoEvents();}label1.Text += sp.ElapsedMilliseconds.ToString() + "ms";//计算耗时}//不使用DoEvents,耗时短,但是全部执行完才能显示textBox文本,建议不使用private void button2_Click(object sender, EventArgs e){Stopwatch sp = new Stopwatch();sp.Start();for (int i= 0; i < 10000; i++){textBox2.Text = i.ToString();}label2.Text += sp.ElapsedMilliseconds.ToString() + "ms";//计算耗时}}
}
三、执行结果:
参考:c#学习笔记之Application.DoEvents应用 - 君莫笑·秋 - 博客园
这篇关于C#【中级篇】Winform中Application.DoEvents的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!