zoukankan      html  css  js  c++  java
  • backgroundworker上运行异步方法,弹出等待窗口

    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 System.Threading;

    namespace SerialportTest
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private ChildForm childForm = null;

            private void button1_Click(object sender, EventArgs e)
            {
                backgroundWorker1.WorkerReportsProgress = true;
                backgroundWorker1.WorkerSupportsCancellation = true;
                backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
                backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
                backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
                childForm = new ChildForm();
                backgroundWorker1.RunWorkerAsync();
                childForm.ShowDialog(this);

            }

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Error != null)
                {
                    MessageBox.Show(e.Error.Message);
                    return;
                }
                if (e.Cancelled)
                {
                    childForm.label1.Text = "Cancelled!";
                    Thread.Sleep(1200);
                    backgroundWorker1.CancelAsync();
                }
                else
                {
                    childForm.label1.Text = "Completed!";
                }
            }

            private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
            {
                childForm.progressBar1.Value = e.ProgressPercentage;//已将子窗体的progressbar设为public;
            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {
                Thread.Sleep(0); //等待子窗体建立完毕;
                for (int i = 0; i < 30; i++)
                {
                    Thread.Sleep(200);
                    backgroundWorker1.ReportProgress(i * 100 / 30);
                    if (e.Cancel == true)
                    {
                        backgroundWorker1.CancelAsync();
                    }
                }
                backgroundWorker1.ReportProgress(100);

                serialPort1.WriteLine("\n");
                Thread.Sleep(2000);
                MessageBox.Show(serialPort1.ReadExisting());
                //这里老是为空.我的串口不用backgroundworker的话(调用一个普通方法)可以测试通过!
                //我在考虑是不是改为同步阻塞读取,因为ReadExisting()是异步读取.

            }
        }
    }
  • 相关阅读:
    PHP导出数据到淘宝助手CSV的方法分享
    创业日志:壹百款购物客户中心正式上线啦!
    THINKPHP+JS缩放图片式截图的实现
    入园3年来的感慨
    CentOS 5 全攻略 一步一步配置详解
    创业日记:进入电子商务领域,需未雨绸缪,更要步步谨慎
    IT商悟读书笔记
    震惊的事情一波接一波的,找自己的FREE
    创业日记:微团队,技术应用思考
    博客园我回来了!
  • 原文地址:https://www.cnblogs.com/voidxy/p/1590483.html
Copyright © 2011-2022 走看看