[英]Using Sleep in VB.NET
我有一個簡單的問題,我有一段代碼發送測試電子郵件,就在此過程即將發生之前,我希望出現一個選取框進度條,表明正在發生某種情況。
但是,在發送測試電子郵件之前,進度條不會出現。
我已經嘗試過使用“睡眠”功能,但是直到發送測試電子郵件后,進度條仍然不會出現。
有人知道為什么嗎?
代碼:(注意:GroupBoxTesting包含進度條)
Private Sub BTMsendmailtest_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTMsendmailtest.Click
GroupBoxTesting.Visible = True
Threading.Thread.Sleep(500)
Try
Dim Mail As New MailMessage
Mail.Subject = "Test email for Email Alerts!"
Mail.To.Add(TXTemailaddy.Text)
Mail.From = New MailAddress(TXTsmtpusr.Text)
Mail.Body = "This is a test message from Email Alerts!" & Environment.NewLine & Environment.NewLine & "If you are reading this, then Email Alerts! is properly configured."
Dim SMTP As New SmtpClient(TXTsmtpsvr.Text)
If CHKEnableSSL.Checked = True Then
SMTP.EnableSsl = True
Else
SMTP.EnableSsl = False
End If
SMTP.Credentials = New System.Net.NetworkCredential(TXTsmtpusr.Text, TXTsmtppwd.Text)
SMTP.Port = TXTsmtpport.Text
SMTP.Send(Mail)
SendingPB.Value = False
MessageBox.Show("A test email has been sent to " & TXTemailaddy.Text & " from " & TXTsmtpusr.Text & "." & Environment.NewLine & Environment.NewLine & "If you did not recieve an email, please check your settings and try again.", "Test Email")
GroupBoxTesting.Visible = False
Catch ex1 As Exception
SendingPB.Value = False
GroupBoxTesting.Visible = False
MessageBox.Show(ex1.Message)
Return
End Try
End Sub
您已告知UI線程(當前正在運行代碼的線程)進入睡眠狀態,因此它進入睡眠狀態並且在指定時間內不執行任何操作。 即使沒有Sleep
,也可能是UI線程太忙於發送電子郵件以更新字幕進度條的顯示。
您可以使用BackgroundWorker發送電子郵件,也可以使用SmtpClient.SendAsync方法 。 后者包括一個例子。 我如何異步發送電子郵件中還有另一個示例? 。
在處理和寫入超過20,000行時,將Excel Automation與VB.Net應用程序一起使用時出現錯誤。 我使用此代碼解決了該問題。
' ---- Write the Row to the Worksheet
Threading.Thread.Sleep(1000)
lstrStartingColRow = "A" & mlngIdx.ToString
If .InsertArrayIntoRow(istrWorksheetName:=lstrWorksheetName,
istrStartingCell:=lstrStartingColRow,
istrArrayOfData:=mstrExcelRow) = False Then
Throw New Exception("Insert Error *************************")
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.