最近需要做一个自动刷新数据的功能。一般来说,自动刷新数据可以采用System.Windows.Forms.Timer来实现,不过我这次尝试着使用控制线程的方式实现自动刷新。
自动刷新效果如下图:
这个Demo程序,点击“查询”按钮可以立即手动刷新,勾选“开启自动刷新”复选框后,可以决定是否自动刷新,“暂离”和“回归”按钮,用于模拟窗体失去焦点的情况,点击“暂离”后,如果当前开启了自动刷新,则暂停自动刷新,点击“回归”按钮后,如果当前开启了自动刷新,则启用自动刷新。
程序中的控件名称如下:
这个Demo使用线程(Thread)每隔1秒都会随机生成DataGridView的数据源,并通过信号对线程进行控制。
程序代码如下:
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;
///
/// 用于控制线程的暂停和继续
///
AutoResetEvent areRefresher = new AutoResetEvent(false);
///
/// 控制标志:暂停线程
///
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();
}
}
}
附:demo下载地址在 http://pan.baidu.com/s/1pJJ4vd5
END