• 条码打印异步调用


           做了将近一年的进销存软件产品,今天无意中发现了一个对象

    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

    }
    }

    好,现在客户端也组建好,就可以看到我们上面的效果图了,大家感觉试一试,希望对大家有所帮助,

    因为时间比较匆忙,今天就写在这,后期会对其进行更详细的述说,也会发表更多的文章,希望大家一起

    来学习。

    如需转载,但请注明文章来源和超链接等版权信息,谢谢合作!
  • 相关阅读:
    【Azure Redis 缓存】Azure Redis 功能性讨论二
    【Azure Developer】如何用Microsoft Graph API管理AAD Application里面的Permissions
    【Azure 环境】通过Python SDK收集所有订阅简略信息,例如订阅id 名称, 资源组及组内资源信息等,如何给Python应用赋予相应的权限才能获取到信息呢?
    【Azure 应用服务】App Service与APIM同时集成到同一个虚拟网络后,如何通过内网访问内部VNET的APIM呢?
    【Azure 云服务】如何从Azure Cloud Service中获取项目的部署文件
    【Azure Redis 缓存】Azure Redis 异常
    【Azure 微服务】基于已经存在的虚拟网络(VNET)及子网创建新的Service Fabric并且为所有节点配置自定义DNS服务
    【Azure Redis 缓存】遇见Azure Redis不能创建成功的问题:至少一个资源部署操作失败,因为 Microsoft.Cache 资源提供程序未注册。
    【Azure Redis 缓存】如何得知Azure Redis服务有更新行为?
    【Azure API 管理】在 Azure API 管理中使用 OAuth 2.0 授权和 Azure AD 保护 Web API 后端,在请求中携带Token访问后报401的错误
  • 原文地址:https://www.cnblogs.com/zhenlin/p/1891291.html
Copyright © 2020-2023  润新知