使用调度程序更新WPF进度条

时间:2011-05-17 19:49:03

标签: c# wpf multithreading progress-bar

我正在尝试使用调度程序更新进度条,但不知何故无法考虑将dispatcher.Invoke放在哪里以及在其中传递的内容。

我试图导入文件,需要向用户显示使用进度条导入的文件数量。

所以我有一个代表:

public delegate void DelegateA(ProgressClass progressClass);

我调用委托并传递函数进行调用。

DelegateA(FunctionA);

因此,在导入每个文件时,它会调用FunctionA。

private void FunctionA(ProgressClass progressClass)
{
    **//Put dispatcher.invoke here?**
    progressbar.updateprogress(progressclass);
    progressbar.show();
}

progressclass有两个属性,用于设置进度条的值(已处理的数量)和要处理的项目总数。

我无法理解在InvokeMethod中传递的委托方法(THreadPriority,委托方法)?

对不起,如果不清楚的话。

3 个答案:

答案 0 :(得分:7)

如果您尝试从某些非UI线程更新UI,则可以执行以下操作..

//here progress bar is a UIElement
progressBar.Dispatcher.BeginInvoke(
           System.Windows.Threading.DispatcherPriority.Normal
           , new DispatcherOperationCallback(delegate
                   {
                       progressBar1.Value = progressBar1.Value + 1;
                       //do what you need to do on UI Thread
                       return null;
                   }), null);

此代码取自关于从后台线程更新UI的好post

答案 1 :(得分:2)

我假设您已经启动了后台线程来导入文件。您应该考虑使用BackgroundWorker,它是轻量级的,并且内置了一种机制来报告使用事件的进度(例如,ProgressBar)。

如果要在处理过程中的任何位置使用新线程,只需声明委托,向目标添加函数,然后调用Dispatcher.BeginInvoke:

Dispatcher.BeginInvoke(DispatcherPriority.Normal, new UpdateProgressDelegate(UpdateProgress), myProgressData);

//...

private delegate void UpdateProgressDelegate(ProgressClass progressClass);

void UpdateProgress(ProgressClass progressClass)
{
    progressbar.updateprogress(progressclass);
    progressbar.show(); 
}

答案 2 :(得分:0)

以下是带有进度条的CSV阅读器的代码

private void InsertCSVRecords(DataTable csvdt)         {

        connection();
        //creating object of SqlBulkCopy    
        SqlBulkCopy objbulk = new SqlBulkCopy(con);
        //assigning Destination table name    
        objbulk.DestinationTableName = "membershipsample";
        //Mapping Table column    
        objbulk.ColumnMappings.Add("GPO_NAME", "GPO_NAME");
        objbulk.ColumnMappings.Add("Ship_To", "Ship_To");
        objbulk.ColumnMappings.Add("Location_Type", "Location_Type");
        objbulk.ColumnMappings.Add("Bill_To", "Bill_To");
        objbulk.ColumnMappings.Add("GPO_CUST_ID", "GPO_CUST_ID");
        objbulk.ColumnMappings.Add("PRI_AFL_FLG", "PRI_AFL_FLG");
        objbulk.ColumnMappings.Add("MCK_GPO_PGM_TYPE", "MCK_GPO_PGM_TYPE");
        objbulk.ColumnMappings.Add("MCK_GPO_PGM_SUB_TYPE", "MCK_GPO_PGM_SUB_TYPE");
        objbulk.ColumnMappings.Add("MCK_MBRSH_EFF_DT", "MCK_MBRSH_EFF_DT");
        objbulk.ColumnMappings.Add("CUST_MAILING_NAME", "CUST_MAILING_NAME");
        objbulk.ColumnMappings.Add("ADDRESS1", "ADDRESS1");
        objbulk.ColumnMappings.Add("ADDRESS2", "ADDRESS2");
        objbulk.ColumnMappings.Add("CITY", "CITY");
        objbulk.ColumnMappings.Add("STATES", "STATES");
        objbulk.ColumnMappings.Add("POSTAL_CODE", "POSTAL_CODE");
        objbulk.ColumnMappings.Add("ACCT_MGR_NAME", "ACCT_MGR_NAME");

        con.Open();
        objbulk.WriteToServer(csvdt);
        con.Close();


    }

    private void button_Click(object sender, RoutedEventArgs e)
    {


        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();



        // Set filter for file extension and default file extension 
        dlg.DefaultExt = ".csv";
        dlg.Filter = "CSV Files (*.cvs)|*.csv|Excel Files (*.xlsx)|*.xlsx";

        // dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
        //dlg.Filter = "CSV Files (*.csv)";

        // Display OpenFileDialog by calling ShowDialog method 
        Nullable<bool> result = dlg.ShowDialog();


        // Get the selected file name and display in a TextBox 
        if (result == true)
        {
            // Open document 
            string filename = dlg.FileName;
            textBox.Text = filename;
            // readCSV(filename);
        }


    }
    private void readCSV(string Path)
    {
        using (var reader = new StreamReader(Path))
        {
            List<string> listA = new List<string>();
            List<string> listB = new List<string>();
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                var values = line.Split(';');

                if (!String.IsNullOrEmpty(values[0]))
                    listA.Add(values[0]);
                if (values.Length > 2 && !String.IsNullOrEmpty(values[1]))
                    listB.Add(values[1]);
            }
        }

    }

    private void Bk_DoWork(object sender, DoWorkEventArgs e)
    {


        // txtStatus.Visibility = System.Windows.Visibility.Visible;
        DispatcherTimer timer = new DispatcherTimer();
        //Creating object of datatable  
        DataTable tblcsv = new DataTable();
        //creating columns  
        tblcsv.Columns.Add("GPO_NAME");
        tblcsv.Columns.Add("Ship_To");
        tblcsv.Columns.Add("Location_Type");
        tblcsv.Columns.Add("Bill_To");
        tblcsv.Columns.Add("GPO_CUST_ID");
        tblcsv.Columns.Add("PRI_AFL_FLG");
        tblcsv.Columns.Add("MCK_GPO_PGM_TYPE");
        tblcsv.Columns.Add("MCK_GPO_PGM_SUB_TYPE");
        tblcsv.Columns.Add("MCK_MBRSH_EFF_DT");
        tblcsv.Columns.Add("CUST_MAILING_NAME");
        tblcsv.Columns.Add("ADDRESS1");
        tblcsv.Columns.Add("ADDRESS2");
        tblcsv.Columns.Add("CITY");
        tblcsv.Columns.Add("STATES");
        tblcsv.Columns.Add("POSTAL_CODE");
        tblcsv.Columns.Add("ACCT_MGR_NAME");

        // DispatcherTimer timer = new DispatcherTimer();
        string url=string.Empty;
        this.Dispatcher.Invoke(() => { url = textBox.Text; });
        string ReadCSV = File.ReadAllText(url);
        //spliting row after new line  url
        int i = 0;
        DataTable dt = GetMemberTable();
        var worker = sender as BackgroundWorker;
        int rows = ReadCSV.Split('\n').Length / 100;
        int totalRows = ReadCSV.Split('\n').Length;
        foreach (string csvRow in ReadCSV.Split('\n'))
        {

            i++;
           // Thread.Sleep(1000);
                   worker.ReportProgress((i/rows + 1), string.Format("This is new one"));
            this.Dispatcher.Invoke(() => { label.Content = i + " rows have been added or updated"; });
            bool Check = (csvRow.Length > 0) ? Exist(csvRow.Split(',')[1].ToString(), dt) : true;
            if (i > 1 && !string.IsNullOrEmpty(csvRow))
            {
                //Adding each row into datatable  

                int count = 0;
                if (!Check)
                {
                    tblcsv.Rows.Add();
                    foreach (string FileRec in csvRow.Split(','))
                    {

                        if (count >= 15)
                        {
                            tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
                        }
                        else
                        {
                            tblcsv.Rows[tblcsv.Rows.Count - 1][count] = FileRec;
                            count++;
                        }
                        //                  //Calling insert Functions 
                        //                  Dispatcher.Invoke(
                        //new System.Action(() => downloadProgress.Value = i)
                        //);


                    }

                }


            }


        }
        if (tblcsv.Rows.Count > 0)
        {
            InsertCSVRecords(tblcsv);
        }
        worker.ReportProgress(100, "Done");
        this.Dispatcher.Invoke(() => { textBox.Text = string.Empty; });
        //textBox.Text = string.Empty;
        this.Dispatcher.Invoke(()=> { label.Content = totalRows + " rows have been added or updated"; });
        this.Dispatcher.Invoke(() => { button1.IsEnabled = false; });
        //button1.IsEnabled = false;
    }
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        BackgroundWorker bk = new BackgroundWorker();
        bk.RunWorkerCompleted += Bk_RunWorkerCompleted;
        bk.WorkerReportsProgress = true;
        bk.DoWork += Bk_DoWork;
        bk.ProgressChanged += Bk_ProgressChanged;
        bk.RunWorkerAsync();


    }

    private void Bk_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        this.Dispatcher.Invoke(() => { downloadProgress.Value = e.ProgressPercentage; });
        //downloadProgress.Value = e.ProgressPercentage;
    }

    //private void Bk_DoWork(object sender, DoWorkEventArgs e)
    //{
    //    AddData();
    //    var worker = sender as BackgroundWorker;
    //    worker.ReportProgress(0, string.Format("This is new one"));
    //    for(int i=0;i<10;i++)
    //    {
    //        Thread.Sleep(1000);
    //        worker.ReportProgress((i + 1) * 10, string.Format("This is new one"));
    //    }

    //    worker.ReportProgress(100, "Done");

    //}

    private void Bk_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("All Done");
        this.Dispatcher.Invoke(() => { downloadProgress.Value = 0; });
    }