繁体   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