简体   繁体   中英

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

In my application i want to Show Progress Bar In Display While User click On Specific Button,

I implemented Below Code

 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; 
    }

But its not Showing Properly.. its takes to much time To start this Progress bar,, its Start Before Just End Of my these function and close Within 2 secound

You can use BackgroundWorker , it has support for progress indication.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM