自动刷新工具
【核心代码】using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace DataRefresher{ public partial class FormMain : Form { //刷新线程 Thread thrRefresher; /// <summary> /// 用于控制线程的暂停和继续 /// </summary> AutoResetEvent areRefresher = new AutoResetEvent(false); /// <summary> /// 控制标志 /// </summary> bool bSign = true; bool bSignAlive = true; public FormMain() { InitializeComponent(); } private void FormMain_Load(object sender, EventArgs e) { thrRefresher = new Thread(new ThreadStart(KeepOnRefreshData)); chkAutoRefresh.Checked = true; } private void btnRefresh_Click(object sender, EventArgs e) { dgvData.DataSource = GetNewData(); } private void chkAutoRefresh_CheckedChanged(object sender, EventArgs e) { //用于线程刷新模式的启动和暂停 if (chkAutoRefresh.Checked) { bSign = true; //第一次勾选时启动进程 if (thrRefresher.ThreadState == ThreadState.Unstarted) { thrRefresher.Start(); areRefresher.Set(); } areRefresher.Set(); } else { bSign = false; } } //用于线程调用:不断刷新dgv private void KeepOnRefreshData() { while (true) { //如果外部窗体关闭,则从内部终止线程 if (!bSignAlive) { break; } RefreshData(dgvData, GetNewData()); //执行一次后暂停一个信息获取周期的时间 for (int i = 0; i < 10; i ) { if (bSignAlive) { Thread.Sleep(100); } else { break; } } //如果外部窗体关闭,则从内部终止线程 if (!bSignAlive) { break; } //如果接收到暂停标志,则暂停线程 if (!bSign) { areRefresher.WaitOne(); } } } //给一个DataGridView赋值 private delegate void RefreshDataDelegate(DataGridView dgv, DataTable dt); private void RefreshData(DataGridView dgv, DataTable dt) { if (this.InvokeRequired) { this.Invoke(new RefreshDataDelegate(this.RefreshData), dgv, dt); } else { dgv.DataSource = dt; } } private DataTable GetNewData() { DataTable dt = new DataTable(); dt.Columns.Add("序号"); dt.Columns.Add("名称"); dt.Columns.Add("力量"); dt.Columns.Add("敏捷"); dt.Columns.Add("智力"); Random random = new Random(DateTime.Now.Millisecond); dt.Rows.Add("1", "秃驴", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("2", "独眼龙", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("3", "三脚猫", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("4", "绿帽王八", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("5", "四眼田鸡", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("6", "地头蛇", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("7", "单身狗", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("8", "替罪羊", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); dt.Rows.Add("9", "黄牛", random.Next(50, 100), random.Next(50, 100), random.Next(50, 100)); return dt; } private void btnLeave_Click(object sender, EventArgs e) { if (chkAutoRefresh.Checked) { bSign = false; } } private void btnReturn_Click(object sender, EventArgs e) { if (chkAutoRefresh.Checked) { bSign = true; areRefresher.Set(); } } private void FormMain_FormClosing(object sender, FormClosingEventArgs e) { bSignAlive = false; areRefresher.Set(); thrRefresher.Join(); } }}
评论