Click here to Skip to main content
14,922,078 members
Please Sign up or sign in to vote.
3.00/5 (1 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:

C#
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
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);

Check this out and adapt ProgressBar[^]
Or check out my example, note the messagebox should only be shown on completion of the backgroundworker thread:
C#
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.");
        }
    }
}
   
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
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.
   

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900