Quantcast
Viewing all articles
Browse latest Browse all 12583

Progress Bar Should end when Email is sent.

Hi,

I hope all of you are fine.

Well I am facing problem and searching for the solution the whole day but didn't find solution, although I am very close to my solution, but due to lack of library knowledge I can't do it.

The problem is that when user click on email password then a new password is sent to the default email address, but it takes some time, I have used the Timer but it doesn't work professionally, bcz some times process completes and shows its completion dialogue box before the progress bar ends and some times progress bar ends and continue again, but the process is still in its processing. I know its happening because there is no link between the process and the timer.

I think my problem will be solved if I use the event handler backgroundWorker1_ProgressChanged, and this event is automatically called when we usebackgroundWorker1.ReportProgress(); in the backgroundWorker1_DoWork event,the problem is I do not know what parameters should I pass in thebackgroundWorker1.ReportProgress(); which will update the progress bar inbackgroundWorker1_ProgressChanged

Here is the block of code I am working on

//When clicked on a button
private void btnEmailPassword_Click(object sender, EventArgs e)
        {
            timer1.Start();
            backgroundWorker1.RunWorkerAsync();
        }

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
              //using Gmail server to send new password
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("abc@xyz.com");
                mail.To.Add("abc@xyz.com");
                mail.Subject = "Test Mail";
                mail.Body = "New Password for " + txtUserName.Text.Trim() + " is " + RandomPassword.Generate();

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("abc@xyz.com", "123456789");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            // What parameters should I pass here?
            backgroundWorker1.ReportProgress();
        }

               // so far i am using this

private void timer1_Tick(object sender, EventArgs e)
        {
            if (progressBar1.Value == progressBar1.Maximum)
            {
                progressBar1.Value = pBar.Minimum;
                return;
            }
            else
                progressBar1.PerformStep();
        }


private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            timer1.Stop();
            MessageBox.Show("New Password Sent");
        }

        // want to use this event
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

Will be waiting for soonest reply.

Thanks in Advance.


Viewing all articles
Browse latest Browse all 12583

Trending Articles