比较DoEvents()与refresh()和两个都不使用的效率
用一个循环来验证(100000次),以下是代码.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsAppDoEvents
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString() +" "+ DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox1.Text = q.ToString();
//Application.DoEvents();//实时响应文本框中的值
}
label3.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox2.Text = q.ToString();
Application.DoEvents();//实时响应文本框中的值
}
label4.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox3.Text = q.ToString();
//Application.DoEvents();//实时响应文本框中的值
textBox3.Refresh();
}
label6.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsAppDoEvents
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString() +" "+ DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox1.Text = q.ToString();
//Application.DoEvents();//实时响应文本框中的值
}
label3.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
label2.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox2.Text = q.ToString();
Application.DoEvents();//实时响应文本框中的值
}
label4.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
private void button3_Click(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
for (int q = 0; q < 100000; q++)
{
textBox3.Text = q.ToString();
//Application.DoEvents();//实时响应文本框中的值
textBox3.Refresh();
}
label6.Text = DateTime.Now.ToLongTimeString() + " " + DateTime.Now.Millisecond.ToString();
}
}
}
效果图如下:
第一个用了3875毫秒
第二个用了14078毫秒
第三个用了12000毫秒
效率高低很明显.