[英]Using Sleep in VB.NET
I have a kind of simple issue, I have a section of code that sends out a test email and just before this process occurs, I'd like a marquee progress bar to appear, indicating that something is happening. 我有一个简单的问题,我有一段代码发送测试电子邮件,就在此过程即将发生之前,我希望出现一个选取框进度条,表明正在发生某种情况。
However, the progress bar does not appear until the test email has been sent. 但是,在发送测试电子邮件之前,进度条不会出现。
I've tried experimenting with the Sleep function, but the progress bar still wont appear until after the test email sends. 我已经尝试过使用“睡眠”功能,但是直到发送测试电子邮件后,进度条仍然不会出现。
Does anyone know why? 有人知道为什么吗?
Code: (Note: GroupBoxTesting contains the Progress bar) 代码:(注意: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
You've told the UI thread (which is the thread your code as it is is currently running on) to go to sleep, so it goes to sleep and doesn't do anything for the specified time. 您已告知UI线程(当前正在运行代码的线程)进入睡眠状态,因此它进入睡眠状态并且在指定时间内不执行任何操作。 Even without the
Sleep
, it may be that the UI thread is too busy sending the email to update the display of the marquee progress bar. 即使没有
Sleep
,也可能是UI线程太忙于发送电子邮件以更新字幕进度条的显示。
You could either use a BackgroundWorker to send the email, or use the SmtpClient.SendAsync Method . 您可以使用BackgroundWorker发送电子邮件,也可以使用SmtpClient.SendAsync方法 。 The latter includes an example;
后者包括一个例子。 there is another example at How do I send email asynchronously?
我如何异步发送电子邮件中还有另一个示例? .
。
I had the error using Excel Automation with a VB.Net application while processing and writing over 20,000 rows. 在处理和写入超过20,000行时,将Excel Automation与VB.Net应用程序一起使用时出现错误。 I used this code to resolve the issue.
我使用此代码解决了该问题。
' ---- 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.