C#中具有IF条件的睡眠线程

时间:2018-08-31 07:37:35

标签: c# multithreading

在C#Windows窗体中,我想在将数据填充到数据网格视图之前设置带有循环和线程的通知标签。为此,我尝试使用以下方法和线程。

我的主题是

public void Run()
    {
        System.Windows.Forms.MessageBox.Show("this is System Thread");
        m = new Dashboard();
        string text = "";
        Int32 a = 0;
        do
        {

            Thread.Sleep(1000);

            notiCount++;
            /*Notifications ne = new Notifications(m.loadNotification);
            ne.Invoke(text);*/


            //Thread.Sleep(1000);
        }
        while (notiCount <= 10) ;
    }

我的方法是

 private void loadINtransfer(string status)
    {
        Int32 x = 0;
        string text="";
        SystemThreadings s = new SystemThreadings();
        Thread t = new Thread(s.Run);
        t.Start();

        Thread.Sleep(100);
        do
        {
            if (s.notiCount == 0)
            {
                text = "Request Data";
                Thread.Sleep(1000);
                this.loadNotification(text);
                // MessageBox.Show("request");

            }
            else if (s.notiCount == 3)
            {
                text = "Fetching Data";
                Thread.Sleep(1000);
                //MessageBox.Show("request");
                this.loadNotification(text);
            }
            else if (s.notiCount == 6)
            {
                text = "Loading Data";
                Thread.Sleep(1000);
                this.loadNotification(text);
                //MessageBox.Show("loading");

            }
            else if (s.notiCount == 9)
            {
                text = "Done";
                Thread.Sleep(1000);
                this.loadNotification(text);
                //MessageBox.Show("done");
            }
            //Thread.Sleep(200);
        }
        while (s.notiCount != 10);

        //Thread.Sleep(5000);
        MessageBox.Show("attach");
        dt = dl.loadTransfer(status);   
        dgvDashboard.AutoGenerateColumns = false;
        dgvDashboard.DataSource = dt;
    }

,但仅显示Done部分。其他,如果零件通过但未显示。我想等待所有零件一段时间,然后再将数据加载到网格视图。我怎样才能做到这一点。请帮助我。

1 个答案:

答案 0 :(得分:0)

正如我在评论中提到的那样,这非常适合BackgroundWorker的后代用例。我创建了一个新的Windows Forms应用程序,添加了一个按钮和一个标签(我不必费心将它们重命名),然后双击该按钮以添加一个事件处理程序。添加BackgroundWorker代码后,Form1.cs的外观如下:

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {
            var bw = new BackgroundWorker();
            bw.DoWork += Bw_DoWork;
            bw.WorkerReportsProgress = true;
            bw.ProgressChanged += Bw_ProgressChanged;
            bw.RunWorkerCompleted += Bw_RunWorkerCompleted;
            bw.RunWorkerAsync();
        }

        private void Bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("attach");
            //dt = dl.loadTransfer(status);
            //dgvDashboard.AutoGenerateColumns = false;
            //dgvDashboard.DataSource = dt;
        }

        private void Bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.ProgressPercentage == 0)
            {
                label1.Text = "Request Data";
            }
            else if (e.ProgressPercentage == 3)
            {
                label1.Text = "Fetching Data";
            }
            else if (e.ProgressPercentage == 6)
            {
                label1.Text = "Loading Data";
            }
            else if (e.ProgressPercentage == 9)
            {
                label1.Text = "Done";
            }
        }

        private void Bw_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            int notiCount = 0;
            do
            {

                Thread.Sleep(1000);

                notiCount++;

                worker.ReportProgress(notiCount);
            }
            while (notiCount <= 10);
        }
    }
}

请注意,我们不必睡觉或等待UI线程。取得进展后,我们会通知,并且可以访问用户界面进行更新。工作完成后,我们会再次收到通知。

我们仅有的Thread.Sleep在后​​台工作人员中,我认为有资格进行实际有用的工作。

绝对不应该在任何受约束的上下文(例如UI线程)中进行任何Thread.Sleep调用,其中UI线程不可能有其他其他个线程可用于阻止您使用您的线程的其他用途。在现代的多线程应用程序中,即使Sleep设置线程池线程也充当着“团队合作者”的角色。