簡體   English   中英

如何在c#中執行方法時在顯示中顯示進度條

[英]how to Show progress Bar In Display While Executing Method in c#

在我的應用程序中,我想在用戶單擊“特定按鈕”時顯示“進度條”,

我實現了以下代碼

 private bool  ImportData()
    {
        bool result = false;
        //Thread oThread = new Thread(new ThreadStart(frmWaitShow));
        try
        {
            Thread backgroundThread = new Thread(
            new ThreadStart(() =>
            {
            //oThread.Start();

                for (int n = 0; n < 100; n++)
                {
                    Thread.Sleep(50);
                    progressBar1.BeginInvoke(new Action(() => progressBar1.Value = n));
                }

            }
              ));
            backgroundThread.Start();
            // Progress Bar Should Start From here
            intdevid = int.Parse(cmbDeviceName.SelectedValue.ToString());
            FetchDevicedata(intdevid);  // Fetch Remove Device Info from SQL database
            //FTPTCompletedBatchTransfer();
            FetchMaxReportId();
            GetFTPFile(strDeviceIP, strDeviceUsername, strDevicePwd, strDevicePath + "//RunningBatch//RunningBatch.db", "RunningBatch.db"); // Copy RunningBatch.db to Debug Folder from Remote 
            LoadRunningData(); // Get Running Data in dataset from running.db
            DecodeBatchData_R(); // save in batch master and row data table
            GetFTPFile(strDeviceIP, strDeviceUsername, strDevicePwd, strDevicePath + "//CompletedBatch//CompletedBatch.db", "CompletedBatch.db");
            LoadCompletedData();
            DecodeBatchData();
            result = true;
            // Progress Bar Should Stop  here
        }
        catch (Exception ex)
        {
            clsLogs.LogError("Error: " + ex.Message + this.Name + " || ImportData");
            result = false;  
        }
        //oThread.Abort();
        return result; 
    }

但是它不能正確顯示..要花費很多時間才能啟動此進度條,請在我的這些功能剛剛結束之前開始並在2秒內關閉

您可以使用BackgroundWorker ,它支持進度指示。

http://msdn.microsoft.com/en-us/library/System.ComponentModel.BackgroundWorker(v=vs.110).aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM