backgroundworker上運行非同步方法呼叫,彈出等待視窗

來源:互聯網
上載者:User
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()是非同步讀取.

        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.