Progress Bar increment while running

60 views Asked by At

I have a program I created in Visual Studio that has a list of programs and checkboxes for each. You select which programs you want, click a button, and then it silently installs without user interaction. I'm trying to add a progress bar to this, but can't seem to get it working properly.

I start with if...then statements to find which ones are checked...

    If Chk_VC.Checked Then Install_VC()          'Visual C++ Redistributable 
    If Chk_dotnet.Checked Then Install_dotnet()     'Microsoft .Net Desktop Runtime

then run the functions to install the specific program(s)...

Function Install_VC()   'Visual C++ 2015-2022
    arg.FileName = "...installs\Microsoft\Framework\VC_redist_2015-2022_14.38.33130_x64.exe"
    arg.Arguments = "/install /passive /norestart"
    Process.Start(arg).WaitForExit()
    Return True
End Function

Function Install_dotnet()
    arg.FileName = "...\installs\Microsoft\Framework\Microsoft .Net Desktop Runtime\windowsdesktop-runtime-6.0.25-win-x64.exe"
    arg.Arguments = "/install /passive /norestart"
    Process.Start(arg).WaitForExit()
    Return True
End Function

I've tried both ProgressBar1.PerformStep() and ProgressBar1.Increment(1) in button_click and individual Procedures. The progress bar only progresses after everything is done running.

So I thought I could try refreshing the progress bar or form itself with ProgressBar1.Refresh() and Application.DoEvents(), but still doesn't display properly.

Any help would be greatly appreciated! Thank you.

EDIT: Created two checkboxes for testing, one with notepad and one with calc.

Sub Install_notepad()
    arg.FileName = "notepad"
    Dim x = WaitForProcess(arg)
    'Thread.Sleep(1000)
End Sub

Sub Install_calc()
    arg.FileName = "calc"
    Dim x = WaitForProcess(arg)
    'Thread.Sleep(1000)
End Sub

Private Async Function WaitForProcess(ByVal args As ProcessStartInfo) As Task
    Await Task.Run(Sub()
       Process.Start(args)
    End Sub)
    ProgressBar1.PerformStep()
End Function

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
    If CheckBox2.Checked Then Install_notepad()
    If CheckBox1.Checked Then Install_calc()

It opens two notepads or calcs depending on which one is "last" in the code, so for this instance, on Button_Click it opens two Calcs. If I include Thread.Sleep(1000), it works fine (but the progress bar still doesn't).

1

There are 1 answers

9
Idle_Mind On

Try calling WaitForExit() on a different thread so it doesn't block your main UI...

Something like:

Sub Install_VC()   'Visual C++ 2015-2022
    arg.FileName = "...installs\Microsoft\Framework\VC_redist_2015-2022_14.38.33130_x64.exe"
    arg.Arguments = "/install /passive /norestart"
    Dim x = WaitForProcess(arg)
End Sub
Sub Install_dotnet()
    arg.FileName = "...\installs\Microsoft\Framework\Microsoft .Net Desktop Runtime\windowsdesktop-runtime-6.0.25-win-x64.exe"
    arg.Arguments = "/install /passive /norestart"
    Dim x = WaitForProcess(arg)
End Sub

Private Async Function WaitForProcess(ByVal args As ProcessStartInfo) As Task
    Await Task.Run(Sub()
                       Process.Start(args).WaitForExit()
                   End Sub)
    ProgressBar1.PerformStep()
End Function