Created
October 5, 2017 02:52
-
-
Save tamago324/391d140630138d4cc19e8b81490a7bb5 to your computer and use it in GitHub Desktop.
裏で重い処理をしておく - VB.net
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class Form1 | |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click | |
Dim form2 As Form2 = New Form2 | |
form2.Show() | |
End Sub | |
End Class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Class Form2 | |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click | |
Button1.Enabled = False | |
' 進捗報告ができるようにする | |
BackgroundWorker1.WorkerReportsProgress = True | |
' 処理開始 | |
BackgroundWorker1.RunWorkerAsync() | |
End Sub | |
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork | |
' 3秒 | |
For I As Integer = 1 To 100 | |
'30mSecスリープさせる | |
System.Threading.Thread.Sleep(30) | |
'ProgressChangedイベントを発生させる | |
BackgroundWorker1.ReportProgress(I) | |
Next | |
MsgBox("終了!") | |
End Sub | |
' 進捗状況を更新する | |
Private Sub BackgroundWorker1_ProgressChanged(sender As System.Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged | |
'プログレスバーを進捗させる | |
ProgressBar1.Value = e.ProgressPercentage | |
End Sub | |
' バックグラウンド処理完了時の処理 | |
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As System.Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted | |
'バックグラウンド処理が完了したので[START]ボタンを有効にする | |
Button1.Enabled = True | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Form2を閉じても「終了!」というメッセージボックスが出てくるため、処理は続いていると考えられる