BackgroundWorker
后台线程处理,觉得挺管用,所以做了一个很小的Demo(条码打印),界面给人一种很真实的感觉,好,切入正题,
所以我们来看看效果图:
好,相信现在大家都已经看到了这种效果图了,好,我们看看代码的实现:
首先: 我们搭建一个业务逻辑层,类名为:ParamUtil
我们看看代码:
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace Action
{
public class ParamUtil
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cmd"></param>
public ParamUtil(string cmd)
{
}
/// <summary>
/// 构造函数
/// </summary>
/// <param name="cmd"></param>
public ParamUtil()
{
}
/// <summary>
/// 初始化数据
/// </summary>
public DataTable CBind1()
{
DataTable dt = new DataTable();
dt.Columns.Add("a",typeof(string));
dt.Columns.Add("b",typeof(string));
dt.Columns.Add("c",typeof(string));
//造数据
for (int i = 0; i < 100;++i )
{
DataRow dr = dt.NewRow();
dr["a"] = "颜色";
dr["b"] = "尺码";
dr["c"] = "未打印";
dt.Rows.Add(dr);
}
return dt;
}
}
}
好,现在我们的业务逻辑层已经搭建好,我们来看客户端的实现:
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Action;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 设计说明
#endregion
#region 变动记录
//Ver 1.0.0.1 (2010-11-29) Andy20101987 初始化数据
#endregion
#region 变量
/// <summary>
/// 后台线程
/// </summary>
private BackgroundWorker bgw;
/// <summary>
/// 设置datagridview第一行的索引值
/// </summary>
/// <param name="rowindex"></param>
private delegate void ChangeDGVFirstDisplayedScrolling(int rowindex);
#endregion
#region 框架方法
/// <summary>
/// 窗体加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Form1_Load(object sender, EventArgs e)
{
// 实例化
Action.ParamUtil paramutil = new ParamUtil();
DataTable dt = new DataTable();
dt =paramutil.CBind1();
this.bindingSource1.DataMember = dt.TableName;
this.bindingSource1.DataSource = dt;
this.toolStripProgressBar1.Maximum = bindingSource1.Count;
}
#endregion
#region 成员方法
/// <summary>
/// 打印
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnPrint_Click(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "正在打印...";
bgw = new BackgroundWorker();
bgw.WorkerReportsProgress = true;
bgw.WorkerSupportsCancellation = true;
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.ProgressChanged += new ProgressChangedEventHandler(bgw_ProgressChanged);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
bgw.RunWorkerAsync();
}
#endregion
#region 事件
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
bindingSource1.MoveFirst();
int rowPosition = bindingSource1.Position;
while (rowPosition <= bindingSource1.Count)
{
if (bgw.CancellationPending)
break;
this.dataGridView1.Rows[bindingSource1.Position].Selected = true;
DataRowView currentRowView = bindingSource1.Current as DataRowView;
currentRowView.Row["c"] = "正在打印...";
System.Threading.Thread.Sleep(1000);
currentRowView.Row["c"] = "打印完成!";
this.dataGridView1.Rows[rowPosition].DefaultCellStyle.ForeColor = Color.Red;
if (bindingSource1.Position > 6)
{
bgw.ReportProgress(bindingSource1.Position - 6);
}
else
{
bgw.ReportProgress(0);
}
bindingSource1.MoveNext();
rowPosition++;
}
bgw.Dispose();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStop_Click(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "取消打印!";
this.toolStripProgressBar1.Value = 0;
bgw.CancelAsync();
bgw.Dispose();
}
/// <summary>
/// 退出
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnExit_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
dataGridView1.FirstDisplayedScrollingRowIndex = e.ProgressPercentage;
dataGridView1.Refresh();
if (this.toolStripProgressBar1.Value.Equals(bindingSource1.Count))
{
toolStripProgressBar1.Value = bindingSource1.Count;
}
else
{
toolStripProgressBar1.Value++;
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.toolStripStatusLabel1.Text = "打印完成!";
}
#endregion
}
}
好,现在客户端也组建好,就可以看到我们上面的效果图了,大家感觉试一试,希望对大家有所帮助,
因为时间比较匆忙,今天就写在这,后期会对其进行更详细的述说,也会发表更多的文章,希望大家一起
来学习。