skip to Main Content

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 = "...installsMicrosoftFrameworkVC_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 = "...installsMicrosoftFrameworkMicrosoft .Net Desktop Runtimewindowsdesktop-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).

2

Answers


  1. Chosen as BEST ANSWER

    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
    

  2. 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 = "...installsMicrosoftFrameworkVC_redist_2015-2022_14.38.33130_x64.exe"
        arg.Arguments = "/install /passive /norestart"
        Dim x = WaitForProcess(arg)
    End Sub
    Sub Install_dotnet()
        arg.FileName = "...installsMicrosoftFrameworkMicrosoft .Net Desktop Runtimewindowsdesktop-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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search