Search code examples
vb.netprogress-bar

Progress Bar increment while running


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).


Solution

  • Thanks to Idle_Mind for pushing me in the right direction. So far this seems to work for my needs/environment. (Note I used notepad and calc for testing, but it works with my other apps so far).

    Dim arg As New ProcessStartInfo()
    
        Sub Install_notepad()
            arg.FileName = "notepad"
            arg.Arguments = ""
    
            Dim x = WaitForProcess(arg)
        End Sub
    
        Sub Install_calc()
            arg.FileName = "calc"
            Dim x = WaitForProcess(arg)
        End Sub
    
        Function WaitForProcess(args As ProcessStartInfo)
            Process.Start(args).WaitForExit()
            ProgressBar1.PerformStep()
            Return True
        End Function
    
    Private Sub BtnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
            If CheckBox2.Checked Then Install_notepad()     'TEST2
            If CheckBox1.Checked Then Install_calc()        'TEST1
    End sub