user3859356
user3859356

Reputation: 739

Picture box Visibility does not change

I have a windows form application in which I send a simple Email to test ID. The problem is the visibility property of picturebox does not change. Can anyone suggest a simple solution?

My button Click

private void BtnSend_Click(object sender, EventArgs e)
{
    try
    {
        pbLoad.Visible = true;
        sendemail();
        pbLoad.Visible = false;

        MessageBox.Show("Mail send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Send Email Function

   public void sendemail()
    {
        SmtpClient client = new SmtpClient("smtp.gmail.com");
        client.Port = 587;
        client.EnableSsl = true;
        client.Timeout = 100000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential(
          "[email protected]", "mypassword");
        MailMessage msg = new MailMessage();
        msg.To.Add(txt_to.Text);
        msg.From = new MailAddress("[email protected]");
        msg.Subject = txt_sub.Text;
        msg.Body = txt_msg.Text;

        //  Attachment data = new Attachment(txt_attach.Text);
        //     msg.Attachments.Add(data);
        // System.Threading.Thread.Sleep(2000);
        client.Send(msg);
    }
}

Upvotes: 1

Views: 230

Answers (2)

Sathish
Sathish

Reputation: 4487

Using Threading concept

private void BtnSend_Click(object sender, EventArgs e)
    {

        try
        {
            pbLoad.Visible = true;

            Thread t=New Thread(sendemail);
            t.start();

         }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    public void sendemail()
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.Port = 587;
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(
              "[email protected]", "mypassword");
            MailMessage msg = new MailMessage();
            msg.To.Add(txt_to.Text);
            msg.From = new MailAddress("[email protected]");
            msg.Subject = txt_sub.Text;
            msg.Body = txt_msg.Text;

            //  Attachment data = new Attachment(txt_attach.Text);
            //     msg.Attachments.Add(data);
            // System.Threading.Thread.Sleep(2000);
            client.Send(msg);

           this.Invoke(new MethodInvoker(Finished))

        }
    }

Private Void Finished() 

{

 pbLoad.Visible = false;
 MessageBox.Show("Mail send");

}

Upvotes: 1

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Because you're not giving the main thread chance to render it. Your main thread is busy in sending the mail. You need to send the mail in another thread. Prefer Task.Run.

Use Async/await feature and call SmtpClient.SendMailAsync which doesn't block the calling thread. So that UI thread will have a time to render the picture box.

Your method should be something like this:

private async void BtnSend_Click(object sender, EventArgs e)
{
    try
    {
        pbLoad.Visible = true;
        await SendEmailAsync();
        pbLoad.Visible = false;

        MessageBox.Show("Mail send");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

public Task SendEmailAsync()
{
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.Port = 587;
    client.EnableSsl = true;
    client.Timeout = 100000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new NetworkCredential(
      "[email protected]", "mypassword");
    MailMessage msg = new MailMessage();
    msg.To.Add(txt_to.Text);
    msg.From = new MailAddress("[email protected]");
    msg.Subject = txt_sub.Text;
    msg.Body = txt_msg.Text;

    //  Attachment data = new Attachment(txt_attach.Text);
    //     msg.Attachments.Add(data);
    // System.Threading.Thread.Sleep(2000);
    return client.SendMailAsync(msg);
}

Upvotes: 1

Related Questions