如何在c#中执行方法时显示进度条

时间:2014-08-18 07:00:31

标签: 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秒内关闭

1 个答案:

答案 0 :(得分:1)