简体   繁体   中英

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(
          "myID@gmail.com", "mypassword");
        MailMessage msg = new MailMessage();
        msg.To.Add(txt_to.Text);
        msg.From = new MailAddress("myID@gmail.com");
        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);
    }
}

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(
      "myID@gmail.com", "mypassword");
    MailMessage msg = new MailMessage();
    msg.To.Add(txt_to.Text);
    msg.From = new MailAddress("myID@gmail.com");
    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);
}

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(
              "myID@gmail.com", "mypassword");
            MailMessage msg = new MailMessage();
            msg.To.Add(txt_to.Text);
            msg.From = new MailAddress("myID@gmail.com");
            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");

}

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