C# 定时器(一分钟执行一次) 打印

在这里插入图片描述
下列代码 自行挑选:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;

namespace BDCQ
{
    public partial class PrintProgram : Form
    {
        delegate void updateDG(DataTable dt);
        public void dataBin(DataTable dt)
        {
            dataGridView1.DataSource = dt;
        }
        BackgroundWorker worker;

        public PrintProgram()
        {
            InitializeComponent();
        }
        

        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                Print();
            }
        }

        private void StartBtn_Click(object sender, EventArgs e)
        {
            PrintBtn.Enabled = false;
            label2.Text = String.Empty;
            worker.RunWorkerAsync();
          
        }
        private void stopBtn_Click(object sender, EventArgs e)
        {
            worker.CancelAsync();
            this.Close();
        }

        private void Print()
        {
          //打印逻辑
           if(是对的逻辑则继续)
           {}
           else
           {
           worker.CancelAsync();//先把定时器停了,在处理其他提示错误啥的逻辑
            }
        }
       private void PrintProgram_Shown(object sender, EventArgs e)
        {
            label2.Text = String.Empty;
            worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.DoWork += new DoWorkEventHandler(worker_DoWork);
        }
    }
}


版权声明:本文为qq_43185226原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。