Click here to Skip to main content
13,148,503 members (63,194 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
my progress bar is not working properly it fills after getting the sucess message but i want showing progress bar with percentage and then sucess message

What I have tried:

private void btnsendmail_Click(object sender, EventArgs e)
        {
            
            progressBar1.Visible = true;
 
            int i;
 
            progressBar1.Minimum = 0;
            progressBar1.Maximum = dgvData.Rows.Count;
 
            for (i = 0; i <= dgvData.Rows.Count; i++)
            {
                progressBar1.Value = i++;
             
            }
 
              if (isTwoFilesCompleted)
            {
 bool isSent = SendMail("Test Mail", "Mail with Attachments");
                        if (isSent)
                        {
                            lblMails.Text = "Completed Row : " + lCount;
                           // Application.DoEvents();
                        }
 
        }
Posted 22-Dec-16 19:39pm
Updated 22-Dec-16 19:56pm
Comments
King Fisher 23-Dec-16 1:57am
   
Can u make your thread sleep 2/3 seconds.
Thread.Sleep(2000);
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Check this out and adapt ProgressBar[^]
Or check out my example, note the messagebox should only be shown on completion of the backgroundworker thread:
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            // get the backgroundworker to report the progress
            backgroundWorker1.WorkerReportsProgress = true;
            // Start the BackgroundWorker.
            backgroundWorker1.RunWorkerAsync();
        }
 

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }
 
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            for (int i = 1; i <= 100; i++)
            {
                // Wait 100 milliseconds.
                Thread.Sleep(100);
                // Report progress.
                backgroundWorker1.ReportProgress(i);
            }
        }
 
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Completed.");
        }
    }
}
  Permalink  
v2
Comments
Member 12742983 23-Dec-16 3:13am
   
this also not showing progress percentage this will wotks same as my prevoius code after suceess it shows
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The problem is than you are updating the progress in the same thread that is supposed to be displaying the current values; and it can't do that until your method returns control to the system so it can process the messages that run "behind the scenes" to actually change the display.

What you need to do is look at using a second thread to do your processing, I suggest you try a BackgroundWorker Class (System.ComponentModel)[^] which provides a built in progress reporting mechanism.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 23 Dec 2016
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100