As said before in order to update the UI, or still make it unblocked and usable while doing really long operation you must do that operation in other thread. You can use BackgroundWorker
to do that. If you don't wanna create new methods only for the new thread you can delegate them so you can do something like this:
Imports System.ComponentModel
Sub DoLongOperation()
Dim bgWorker As New System.ComponentModel.BackgroundWorker()
bgWorker.WorkerReportsProgress = True
AddHandler bgWorker.DoWork,
Sub(sender, args)
For K as integer = 0 to 50
'Here I create tools and add them to a StackPanel in the window
'While doing this I update a progressBar value
bgWorker.ReportProgress(123)
Next
End Sub
AddHandler bgWorker.ProgressChanged,
Sub(sender, args)
ProgressBar.Value = args.ProgressPercentage
End Sub
AddHandler bgWorker.RunWorkerCompleted,
Sub(sender, args)
End Sub
End sub
Since you cannot add/remove or do other things from other thread on the StackPanel
, you can put the item that you want to add into the bgWorker.ReportProgress()
method and then update it from .ProgressChanged
event so it will be like that:
AddHandler bgWorker.DoWork,
Sub(sender, args)
For K as integer = 0 to 50
'Here I create tools and add them to a StackPanel in the window
'While doing this I update a progressBar value
Dim item As Object 'stackpanel's item that you want to add later on. Can be any type
bgWorker.ReportProgress(123, item)
Next
End Sub
AddHandler bgWorker.ProgressChanged,
Sub(sender, args)
ProgressBar.Value = args.ProgressPercentage
StackPanel.Chlidren.Add(args.UserState)
End Sub