Skip to content

Instantly share code, notes, and snippets.

@tamago324
Created October 5, 2017 02:52
Show Gist options
  • Save tamago324/391d140630138d4cc19e8b81490a7bb5 to your computer and use it in GitHub Desktop.
Save tamago324/391d140630138d4cc19e8b81490a7bb5 to your computer and use it in GitHub Desktop.
裏で重い処理をしておく - VB.net
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
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
@tamago324
Copy link
Author

Form2を閉じても「終了!」というメッセージボックスが出てくるため、処理は続いていると考えられる

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment