[英]VB.NET - Shutdown Timer/Sleep Labels
I have the following code, which is supposed to load the form, change the text every second and then run a shutdown command at the end. 我有以下代码,应该加载表单,每秒更改文本,然后最后运行关闭命令。 The cancel button would cancel the shutdown (or just close the form, preventing the shutdown command from running).
取消按钮将取消关闭(或仅关闭表单,以防止运行关闭命令)。
Public Class FRM_SHUTDOWN
Private Sub FRM_SHUTDOWN_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LBL_TIMER.Text = "Shutting Down in 6..."
End Sub
Private Sub FRM_SHUTDOWN_Shown(sender As Object, e As EventArgs) Handles Me.Shown
LBL_TIMER.Text = "Shutting Down in 6..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Shutting Down in 5..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Shutting Down in 4..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Shutting Down in 3..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Shutting Down in 2..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Shutting Down in 1..."
Threading.Thread.Sleep(1000)
LBL_TIMER.Text = "Windows is Shutting Down"
Threading.Thread.Sleep(1000)
'System.Diagnostics.Process.Start("shutdown", "-s -t 0")
MessageBox.Show("Shutdown instant would happen here")
End Sub
Private Sub BTN_CANCEL_Click(sender As Object, e As EventArgs) Handles BTN_CANCEL.Click
'System.Diagnostics.Process.Start("shutdown", "-a")
MessageBox.Show("Cancel shutdown command")
Me.Close()
End Sub
End Class 末级
The problem I'm having is that the form loads and appears like this: 我遇到的问题是表单加载并显示如下:
So it doesn't actually show the 'stop' button or the countdown timer! 因此,它实际上并未显示“停止”按钮或倒数计时器!
Can anyone explain why? 谁能解释为什么?
Your application is busy waiting, so it can't redraw the form (Adding LBL_TIMER.Refresh
will help but is not the correct way to do this). 您的应用程序正忙于等待,因此无法重绘表单(添加
LBL_TIMER.Refresh
会有所帮助,但这不是正确的方法)。 You need to do your waiting in a thread that is separate from the UI thread. 您需要在与UI线程不同的线程中等待。
The easiest way to do this is with a background worker. 最简单的方法是使用后台工作人员。 This handles some of the synchronisation between UI thread and the background thread for you.
这可以为您处理UI线程和后台线程之间的一些同步。
Run the worker when the form loads and then do your waiting in the worker DoWork
method. 加载表单时运行工作程序,然后在工作程序
DoWork
方法中等待。
You can report your progress back using the ReportProgress
method 您可以使用
ReportProgress
方法向您报告进度
Private WithEvents _worker As New BackgroundWorker With
{.WorkerReportsProgress = True, .WorkerSupportsCancellation = True}
Private _cancelled As Boolean = False
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_worker.RunWorkerAsync()
End Sub
Private Sub _worker_DoWork(sender As Object, e As DoWorkEventArgs) Handles _worker.DoWork
_cancelled = False
For i As Integer = 6 To 1 Step -1
CType(sender, BackgroundWorker).ReportProgress(i)
Threading.Thread.Sleep(1000)
If CType(sender, BackgroundWorker).CancellationPending Then Exit Sub
Next
End Sub
Private Sub _worker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles _worker.ProgressChanged
Label1.Text = "Shutting down in " & e.ProgressPercentage
End Sub
Private Sub _worker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles _worker.RunWorkerCompleted
If _cancelled Then
MessageBox.Show("Shutdown was cancelled")
Else
MessageBox.Show("Shutdown instant would happen here")
End If
End Sub
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
_cancelled = True
_worker.CancelAsync()
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.