Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB.NET Progress status while run a EXE file

VB.NET Progress status while run a EXE file

Scheduled Pinned Locked Moved Visual Basic
csharplinuxquestion
21 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Andraw Tang

    Hi, I have a project created using vb.net 2005. I need to collect data and generate a input file for a EXE file which will take 2 min to execute the exe file. I also create a form with progressbar and Stop button, since I cannot get status of the EXE file, I just set the value of the progressbar from 1 ~ 100 then repeat. codes as the following Dim p As New System.Diagnostics.Process() p.StartInfo.FileName = Application.StartupPath & "\" & sAppPath p.StartInfo.Arguments = fileName 'Do not use the system shell to start the program this is so we can hide the command dos window p.StartInfo.UseShellExecute = False p.StartInfo.WorkingDirectory = strOutputFilePath ' Show no dos window if false p.StartInfo.CreateNoWindow = True p.StartInfo.RedirectStandardOutput = True Dim progBarFrm As New FormProgressBar progBarFrm.Show() p.Start '''''''''''''' But when I run the above code, the progressbar value doesn't change. But if I comment out p.Start line, the progressbar works fine. How can I show the progressbar while the process is running, if you have some sample, that's best. Thanks!

    L Offline
    L Offline
    Luc Pattyn
    wrote on last edited by
    #3

    On top of what Dave said, please use PRE tags when showing code snippets, that will improve readability. And tell us where that code is sitting; and if threads are involved, we need to know that too. :)

    Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

    Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

    A 2 Replies Last reply
    0
    • L Luc Pattyn

      On top of what Dave said, please use PRE tags when showing code snippets, that will improve readability. And tell us where that code is sitting; and if threads are involved, we need to know that too. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      A Offline
      A Offline
      Andraw Tang
      wrote on last edited by
      #4

      Davd and Luc, Thanks for your reply. Acutally my code is like the following, I didn't use thread, I am not familiar with it.

          Dim progBarFrm As New FormProgressBar
          progBarFrm.Show()
      
          try
              p.Start()
          Catch ex As Exception
              ShowValidationMessage("Unexception error is caught, please close WinTAP and try again.", "Error")
              progBarFrm.Close()
              Return
          End Try
      
      
          rtnStr = p.StandardOutput.ReadToEnd()
      
          p.WaitForExit(5400000)       
          If Not p.HasExited Then
              p.Kill()
          End If
      
          progBarFrm.Close()
      
      1 Reply Last reply
      0
      • L Luc Pattyn

        On top of what Dave said, please use PRE tags when showing code snippets, that will improve readability. And tell us where that code is sitting; and if threads are involved, we need to know that too. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        A Offline
        A Offline
        Andraw Tang
        wrote on last edited by
        #5

        Hi, Now I modify the code to as the following, but OnProcesited is never called, why?????

        Dim progBarFrm as New FormProgressBar
        
        Private Sub Run\_Tool()
        
            Dim sAppPath As String = ".\\XXXX.exe"
            Dim fileName As String = "xxxxxxxxxxxx"
            Dim p As New System.Diagnostics.Process()  
        
        
            progBarFrm = New FormProgressBar
        
            Try
                p.Start()
                p.EnableRaisingEvents = True
                AddHandler p.Exited, AddressOf OnProcesited
        
                progBarFrm.Show()
                progBarFrm.Refresh()
        
            Catch ex As Exception
                ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")          
                Return
            End Try
        
        
        
        
            ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
        
        End Sub
        Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim p As System.Diagnostics.Process
            p = sender
        
            progBarFrm.Close()
            progBarFrm.Dispose()        
        End Sub
        
        L 1 Reply Last reply
        0
        • A Andraw Tang

          Hi, Now I modify the code to as the following, but OnProcesited is never called, why?????

          Dim progBarFrm as New FormProgressBar
          
          Private Sub Run\_Tool()
          
              Dim sAppPath As String = ".\\XXXX.exe"
              Dim fileName As String = "xxxxxxxxxxxx"
              Dim p As New System.Diagnostics.Process()  
          
          
              progBarFrm = New FormProgressBar
          
              Try
                  p.Start()
                  p.EnableRaisingEvents = True
                  AddHandler p.Exited, AddressOf OnProcesited
          
                  progBarFrm.Show()
                  progBarFrm.Refresh()
          
              Catch ex As Exception
                  ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")          
                  Return
              End Try
          
          
          
          
              ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
          
          End Sub
          Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
              Dim p As System.Diagnostics.Process
              p = sender
          
              progBarFrm.Close()
              progBarFrm.Dispose()        
          End Sub
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #6

          You should prepare the process p completely before calling Start() on it; once launched, it is out of your control. :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          A 1 Reply Last reply
          0
          • L Luc Pattyn

            You should prepare the process p completely before calling Start() on it; once launched, it is out of your control. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            A Offline
            A Offline
            Andraw Tang
            wrote on last edited by
            #7

            Thanks for reply, I modify the code, but still don't work

            L 1 Reply Last reply
            0
            • A Andraw Tang

              Thanks for reply, I modify the code, but still don't work

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #8

              Andraw Tang wrote:

              still don't work

              That is not informative. Anyway, you never made clear how the progress would be reported, as Dave asked. What is changing the ProgressBar's value? where is that code? when is it called? by whom? on what thread? etc. Please explain the situation clearly, provide necessary information, then ask specific questions. :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              A 1 Reply Last reply
              0
              • L Luc Pattyn

                Andraw Tang wrote:

                still don't work

                That is not informative. Anyway, you never made clear how the progress would be reported, as Dave asked. What is changing the ProgressBar's value? where is that code? when is it called? by whom? on what thread? etc. Please explain the situation clearly, provide necessary information, then ask specific questions. :)

                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                A Offline
                A Offline
                Andraw Tang
                wrote on last edited by
                #9

                I thought I already give all the information needed. The Run_Tool() will be called when user click "Run" item in the menu. Inside Run_Tool(), a process will be created to run an exe file, at the same time, a form with progress bar will be created and displayed (let's forget about the progress bar value at this time), I also add an event Exited for the procee, when the event is called, close the form with progress bar, that's it. Now the problem is that the Exited event is never called, so form progBarFrm is never closed.

                 Dim progBarFrm as FormProgressBar
                
                 Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                    Dim p As System.Diagnostics.Process
                    p = sender
                
                    progBarFrm.Close()
                    progBarFrm.Dispose()
                
                  
                End Sub
                
                
                Private Sub Run\_Tool()
                
                    Dim sAppPath As String = ".\\XXXX.exe"
                    Dim fileName As String = "DDDDDDDDDDDDDDDD.txt"
                    Dim p As New System.Diagnostics.Process()
                
                
                
                    p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                    p.StartInfo.Arguments = fileName
                
                    'Do not use the system shell to start the program this is so we can hide the command dos window
                    p.StartInfo.UseShellExecute = False
                    p.StartInfo.WorkingDirectory = strOutputFilePath
                    ' Show no dos window if false
                    p.StartInfo.CreateNoWindow = True
                    p.StartInfo.RedirectStandardOutput = True
                    p.EnableRaisingEvents = True
                    AddHandler p.Exited, AddressOf OnProcesited
                
                
                    progBarFrm = New FormProgressBar
                
                    Try
                        p.Start()
                
                        progBarFrm.Show()
                        progBarFrm.Refresh()
                
                    Catch ex As Exception
                        ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                        'p.Kill()            
                        Return
                    End Try
                
                
                
                
                    ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
                
                End Sub
                

                modified on Tuesday, May 24, 2011 2:21 PM

                L D A 3 Replies Last reply
                0
                • A Andraw Tang

                  I thought I already give all the information needed. The Run_Tool() will be called when user click "Run" item in the menu. Inside Run_Tool(), a process will be created to run an exe file, at the same time, a form with progress bar will be created and displayed (let's forget about the progress bar value at this time), I also add an event Exited for the procee, when the event is called, close the form with progress bar, that's it. Now the problem is that the Exited event is never called, so form progBarFrm is never closed.

                   Dim progBarFrm as FormProgressBar
                  
                   Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                      Dim p As System.Diagnostics.Process
                      p = sender
                  
                      progBarFrm.Close()
                      progBarFrm.Dispose()
                  
                    
                  End Sub
                  
                  
                  Private Sub Run\_Tool()
                  
                      Dim sAppPath As String = ".\\XXXX.exe"
                      Dim fileName As String = "DDDDDDDDDDDDDDDD.txt"
                      Dim p As New System.Diagnostics.Process()
                  
                  
                  
                      p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                      p.StartInfo.Arguments = fileName
                  
                      'Do not use the system shell to start the program this is so we can hide the command dos window
                      p.StartInfo.UseShellExecute = False
                      p.StartInfo.WorkingDirectory = strOutputFilePath
                      ' Show no dos window if false
                      p.StartInfo.CreateNoWindow = True
                      p.StartInfo.RedirectStandardOutput = True
                      p.EnableRaisingEvents = True
                      AddHandler p.Exited, AddressOf OnProcesited
                  
                  
                      progBarFrm = New FormProgressBar
                  
                      Try
                          p.Start()
                  
                          progBarFrm.Show()
                          progBarFrm.Refresh()
                  
                      Catch ex As Exception
                          ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                          'p.Kill()            
                          Return
                      End Try
                  
                  
                  
                  
                      ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
                  
                  End Sub
                  

                  modified on Tuesday, May 24, 2011 2:21 PM

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #10

                  That clearly is not the entire story. Are you waiting for the process to exit somewhere? what is in the "Run clicked" handler? Is your app responsive (can you move its main window, will it repaint) while the process runs? one handler can not execute while another one is still running! :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  A 1 Reply Last reply
                  0
                  • A Andraw Tang

                    I thought I already give all the information needed. The Run_Tool() will be called when user click "Run" item in the menu. Inside Run_Tool(), a process will be created to run an exe file, at the same time, a form with progress bar will be created and displayed (let's forget about the progress bar value at this time), I also add an event Exited for the procee, when the event is called, close the form with progress bar, that's it. Now the problem is that the Exited event is never called, so form progBarFrm is never closed.

                     Dim progBarFrm as FormProgressBar
                    
                     Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                        Dim p As System.Diagnostics.Process
                        p = sender
                    
                        progBarFrm.Close()
                        progBarFrm.Dispose()
                    
                      
                    End Sub
                    
                    
                    Private Sub Run\_Tool()
                    
                        Dim sAppPath As String = ".\\XXXX.exe"
                        Dim fileName As String = "DDDDDDDDDDDDDDDD.txt"
                        Dim p As New System.Diagnostics.Process()
                    
                    
                    
                        p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                        p.StartInfo.Arguments = fileName
                    
                        'Do not use the system shell to start the program this is so we can hide the command dos window
                        p.StartInfo.UseShellExecute = False
                        p.StartInfo.WorkingDirectory = strOutputFilePath
                        ' Show no dos window if false
                        p.StartInfo.CreateNoWindow = True
                        p.StartInfo.RedirectStandardOutput = True
                        p.EnableRaisingEvents = True
                        AddHandler p.Exited, AddressOf OnProcesited
                    
                    
                        progBarFrm = New FormProgressBar
                    
                        Try
                            p.Start()
                    
                            progBarFrm.Show()
                            progBarFrm.Refresh()
                    
                        Catch ex As Exception
                            ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                            'p.Kill()            
                            Return
                        End Try
                    
                    
                    
                    
                        ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
                    
                    End Sub
                    

                    modified on Tuesday, May 24, 2011 2:21 PM

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #11

                    You still have absolutely nothing updating the progress bar. You called Show on the form to show the form with the progress bar on it, but you've got nothing at all that updates the progress bar, so it's not going to do anything. Without seeing the code behind the progressbar form, this is about all we can say.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak

                    A 1 Reply Last reply
                    0
                    • A Andraw Tang

                      I thought I already give all the information needed. The Run_Tool() will be called when user click "Run" item in the menu. Inside Run_Tool(), a process will be created to run an exe file, at the same time, a form with progress bar will be created and displayed (let's forget about the progress bar value at this time), I also add an event Exited for the procee, when the event is called, close the form with progress bar, that's it. Now the problem is that the Exited event is never called, so form progBarFrm is never closed.

                       Dim progBarFrm as FormProgressBar
                      
                       Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                          Dim p As System.Diagnostics.Process
                          p = sender
                      
                          progBarFrm.Close()
                          progBarFrm.Dispose()
                      
                        
                      End Sub
                      
                      
                      Private Sub Run\_Tool()
                      
                          Dim sAppPath As String = ".\\XXXX.exe"
                          Dim fileName As String = "DDDDDDDDDDDDDDDD.txt"
                          Dim p As New System.Diagnostics.Process()
                      
                      
                      
                          p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                          p.StartInfo.Arguments = fileName
                      
                          'Do not use the system shell to start the program this is so we can hide the command dos window
                          p.StartInfo.UseShellExecute = False
                          p.StartInfo.WorkingDirectory = strOutputFilePath
                          ' Show no dos window if false
                          p.StartInfo.CreateNoWindow = True
                          p.StartInfo.RedirectStandardOutput = True
                          p.EnableRaisingEvents = True
                          AddHandler p.Exited, AddressOf OnProcesited
                      
                      
                          progBarFrm = New FormProgressBar
                      
                          Try
                              p.Start()
                      
                              progBarFrm.Show()
                              progBarFrm.Refresh()
                      
                          Catch ex As Exception
                              ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                              'p.Kill()            
                              Return
                          End Try
                      
                      
                      
                      
                          ''Shell(sAppPath, vbNormalFocus) 'other options for starting with
                      
                      End Sub
                      

                      modified on Tuesday, May 24, 2011 2:21 PM

                      A Offline
                      A Offline
                      Andraw Tang
                      wrote on last edited by
                      #12

                      I found only if I clearly call p.Kill(), the Exited event will be triggered, but when I should call the .Kill() for a process? if I use p.WaitForExit(5400000), the program will be hanged to wait for process to exit, the progressbar form will be hanged.

                      D 1 Reply Last reply
                      0
                      • A Andraw Tang

                        I found only if I clearly call p.Kill(), the Exited event will be triggered, but when I should call the .Kill() for a process? if I use p.WaitForExit(5400000), the program will be hanged to wait for process to exit, the progressbar form will be hanged.

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #13

                        The process will drop by itself. You shouldn't have to call Kill on it at all. Once the process you launched quits on its own, the Exited event will be raised. 5400000?? Really? 90 minutes?? You do realize WaitForExit is a blocking call, right? No other code will execute so long as WaitForExit is waiting. Remove this line and just let it run. When the target app is finished, your event handler will get called.

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak

                        A 2 Replies Last reply
                        0
                        • L Luc Pattyn

                          That clearly is not the entire story. Are you waiting for the process to exit somewhere? what is in the "Run clicked" handler? Is your app responsive (can you move its main window, will it repaint) while the process runs? one handler can not execute while another one is still running! :)

                          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                          A Offline
                          A Offline
                          Andraw Tang
                          wrote on last edited by
                          #14

                          That's the whole codes when "Run" is clicked. Yes, I need to waiting for the process to exit, I need to get the return string and display them in the form. That's why I use p.WaitForExit(5400000). But use p.WaitForExit(5400000) will hang progressbar form, if don;t use p.WaitForExit(5400000), when should I close the progressbar form?

                           Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                              Dim p As System.Diagnostics.Process
                              p = sender
                          
                              Dim rtnStr As String = String.Empty
                              **rtnStr = p.StandardOutput.ReadToEnd()**
                          
                              If rtnStr.Contains(" FAST terminated normally.") = True Then
                                  rtnStr = rtnStr & vbCrLf & "Please click Postprocess -> View Output File in menu to view the file."
                                  updateViewMenu()
                              End If
                              '''progBarFrm.Close()
                              '''progBarFrm.Dispose()
                              **
                              Dim frm As New FormShowFASTResult
                              frm.txtFASTResult.Text = rtnStr
                              frm.Button1.Select()
                              frm.ShowDialog()**
                            End Sub
                          
                          1 Reply Last reply
                          0
                          • D Dave Kreskowiak

                            You still have absolutely nothing updating the progress bar. You called Show on the form to show the form with the progress bar on it, but you've got nothing at all that updates the progress bar, so it's not going to do anything. Without seeing the code behind the progressbar form, this is about all we can say.

                            A guide to posting questions on CodeProject[^]
                            Dave Kreskowiak

                            A Offline
                            A Offline
                            Andraw Tang
                            wrote on last edited by
                            #15

                            Progressbar value should be updated using timer inside the form, after call SHOW of the form, the timer will be started, sure you cannot see these codes from inside the function which create the form.

                            1 Reply Last reply
                            0
                            • D Dave Kreskowiak

                              The process will drop by itself. You shouldn't have to call Kill on it at all. Once the process you launched quits on its own, the Exited event will be raised. 5400000?? Really? 90 minutes?? You do realize WaitForExit is a blocking call, right? No other code will execute so long as WaitForExit is waiting. Remove this line and just let it run. When the target app is finished, your event handler will get called.

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak

                              A Offline
                              A Offline
                              Andraw Tang
                              wrote on last edited by
                              #16

                              Sorry calculation error, I already comment out that line. Now I modify the source codes, also remove the event. When "Run" menu is clicked, it will call Run_Tool() inside which it start a precess and open a form, I also show the codes for progressbar form below, but the progressbar color is grayed, no value is shown. Would you please take a look where is wrong? thanks!

                                Private Sub Run\_Tool()
                              
                                  Dim sAppPath As String = ".\\xxxx.exe"
                                  Dim fileName As String = "XXXXXX.TXT"
                                  Dim p As New System.Diagnostics.Process()
                              
                                  Dim rtnStr As String = String.Empty
                              
                                  p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                  p.StartInfo.Arguments = fileName
                              
                                  'Do not use the system shell to start the program this is so we can hide the command dos window
                                  p.StartInfo.UseShellExecute = False
                                  p.StartInfo.WorkingDirectory = strOutputFilePath
                                  ' Show no dos window if false
                                  p.StartInfo.CreateNoWindow = True
                                  p.StartInfo.RedirectStandardOutput = True
                              
                                  Dim progBarFrm As New FormProgressBar
                                  progBarFrm.Show()
                              
                              
                                  Try
                                      p.Start()
                                  Catch ex As Exception
                                      ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                                      'p.Kill()
                                      progBarFrm.Close()
                                      Return
                                  End Try
                              
                              
                                  rtnStr = p.StandardOutput.ReadToEnd()
                              
                                  ''p.WaitForExit(120000)   '= 1.5 min: 1000 millisecond = 1 second
                                  ' if the process doesn't complete within 5400 seconds, kill it
                                  ''If Not p.HasExited Then
                                  ''p.Kill()
                                  ''End If
                              
                                  If rtnStr.Contains(" FAST terminated normally.") = True Then
                                      rtnStr = rtnStr & vbCrLf & "Please click Postprocess -> View Output File in menu to view the file."
                                      updateViewMenu()
                                  End If
                                  progBarFrm.Close()
                              
                                  Dim frm As New FormShowFASTResult
                                  frm.txtFASTResult.Text = rtnStr
                                  frm.Button1.Select()
                                  frm.ShowDialog()      
                              
                              End Sub
                              

                              Source code for the progressbar form as the following

                              Public Class FormProgressBar

                              Private Sub FormProgressBar\_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
                                  Timer1.Stop()
                              End Sub
                              
                              
                              
                              Private Sub FormProgressBar\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                                  Me.CenterToScreen()
                                  ProgressBar1.Mi
                              
                              D 1 Reply Last reply
                              0
                              • D Dave Kreskowiak

                                The process will drop by itself. You shouldn't have to call Kill on it at all. Once the process you launched quits on its own, the Exited event will be raised. 5400000?? Really? 90 minutes?? You do realize WaitForExit is a blocking call, right? No other code will execute so long as WaitForExit is waiting. Remove this line and just let it run. When the target app is finished, your event handler will get called.

                                A guide to posting questions on CodeProject[^]
                                Dave Kreskowiak

                                A Offline
                                A Offline
                                Andraw Tang
                                wrote on last edited by
                                #17

                                If I don't start the process, the progressbar in the form works fine. So the p.Start action hang the progressbar form.

                                 Private Sub Run\_FAST\_Tool()
                                
                                    Dim sAppPath As String = ".\\FAST.exe"
                                    Dim fileName As String = objFASTPrimaryData.FASTPrimaryFileName\_FST
                                    Dim p As New System.Diagnostics.Process()
                                
                                    Dim rtnStr As String = String.Empty
                                
                                    p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                    p.StartInfo.Arguments = fileName
                                
                                    'Do not use the system shell to start the program this is so we can hide the command dos window
                                    p.StartInfo.UseShellExecute = False
                                    p.StartInfo.WorkingDirectory = strOutputFilePath
                                    ' Show no dos window if false
                                    p.StartInfo.CreateNoWindow = True
                                    p.StartInfo.RedirectStandardOutput = True
                                
                                    Dim progBarFrm As New FormProgressBar
                                    progBarFrm.Show()
                                end sub
                                
                                D 1 Reply Last reply
                                0
                                • A Andraw Tang

                                  Sorry calculation error, I already comment out that line. Now I modify the source codes, also remove the event. When "Run" menu is clicked, it will call Run_Tool() inside which it start a precess and open a form, I also show the codes for progressbar form below, but the progressbar color is grayed, no value is shown. Would you please take a look where is wrong? thanks!

                                    Private Sub Run\_Tool()
                                  
                                      Dim sAppPath As String = ".\\xxxx.exe"
                                      Dim fileName As String = "XXXXXX.TXT"
                                      Dim p As New System.Diagnostics.Process()
                                  
                                      Dim rtnStr As String = String.Empty
                                  
                                      p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                      p.StartInfo.Arguments = fileName
                                  
                                      'Do not use the system shell to start the program this is so we can hide the command dos window
                                      p.StartInfo.UseShellExecute = False
                                      p.StartInfo.WorkingDirectory = strOutputFilePath
                                      ' Show no dos window if false
                                      p.StartInfo.CreateNoWindow = True
                                      p.StartInfo.RedirectStandardOutput = True
                                  
                                      Dim progBarFrm As New FormProgressBar
                                      progBarFrm.Show()
                                  
                                  
                                      Try
                                          p.Start()
                                      Catch ex As Exception
                                          ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                                          'p.Kill()
                                          progBarFrm.Close()
                                          Return
                                      End Try
                                  
                                  
                                      rtnStr = p.StandardOutput.ReadToEnd()
                                  
                                      ''p.WaitForExit(120000)   '= 1.5 min: 1000 millisecond = 1 second
                                      ' if the process doesn't complete within 5400 seconds, kill it
                                      ''If Not p.HasExited Then
                                      ''p.Kill()
                                      ''End If
                                  
                                      If rtnStr.Contains(" FAST terminated normally.") = True Then
                                          rtnStr = rtnStr & vbCrLf & "Please click Postprocess -> View Output File in menu to view the file."
                                          updateViewMenu()
                                      End If
                                      progBarFrm.Close()
                                  
                                      Dim frm As New FormShowFASTResult
                                      frm.txtFASTResult.Text = rtnStr
                                      frm.Button1.Select()
                                      frm.ShowDialog()      
                                  
                                  End Sub
                                  

                                  Source code for the progressbar form as the following

                                  Public Class FormProgressBar

                                  Private Sub FormProgressBar\_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
                                      Timer1.Stop()
                                  End Sub
                                  
                                  
                                  
                                  Private Sub FormProgressBar\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                                      Me.CenterToScreen()
                                      ProgressBar1.Mi
                                  
                                  D Offline
                                  D Offline
                                  Dave Kreskowiak
                                  wrote on last edited by
                                  #18

                                  First, ReadToEnd isn't going to give you what you're looking for. It'll block until the console has to something to return, and even then, it'll return block of text, most likely NOT all of the text your app output to the console, and it will only read ONE block of text. Now, if you want to gather all the text the app outputs, and still have a responsive UI (such as the progress bar redrawing itself), you'll have to read the console output asynchronously. Something like:

                                  Private Sub StartConsoleRead(ByRef st As Stream)
                                  Dim rdo As New ReadData
                                  'rdo.s = _conStream.BaseStream
                                  rdo.s = st

                                      Dim consoleReadAsyncCallback As New AsyncCallback(AddressOf ConsoleReadCallback)
                                      \_conStream.BaseStream.BeginRead(rdo.buffer, 0, rdo.buffer.Length, consoleReadAsyncCallback, rdo)
                                  End Sub
                                  
                                  Private Sub ConsoleReadCallback(ByVal asyncResult As IAsyncResult)
                                      Dim rdo As ReadData = DirectCast(asyncResult.AsyncState, ReadData)
                                      Dim bytesRead As Integer = rdo.s.EndRead(asyncResult)
                                      Dim enc As Encoding = Encoding.ASCII
                                      Dim s As String = enc.GetString(rdo.buffer, 0, bytesRead)
                                  
                                      CmdOutput.Invoke(UpdateCmdOutputDelegate, New Object() {s})
                                      StartConsoleRead(rdo.s)
                                  End Sub
                                  

                                  Public Class ReadData
                                  Public buffer(20480) As Byte
                                  Public s As Stream
                                  End Class

                                  Also, you're Timer.Interval on the progress bar form should not be 1. Make it something more reasonable, like 250 to 500 (quarter to half a second). Setting it to one will just make your app hog the CPU.

                                  A guide to posting questions on CodeProject[^]
                                  Dave Kreskowiak

                                  1 Reply Last reply
                                  0
                                  • A Andraw Tang

                                    If I don't start the process, the progressbar in the form works fine. So the p.Start action hang the progressbar form.

                                     Private Sub Run\_FAST\_Tool()
                                    
                                        Dim sAppPath As String = ".\\FAST.exe"
                                        Dim fileName As String = objFASTPrimaryData.FASTPrimaryFileName\_FST
                                        Dim p As New System.Diagnostics.Process()
                                    
                                        Dim rtnStr As String = String.Empty
                                    
                                        p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                        p.StartInfo.Arguments = fileName
                                    
                                        'Do not use the system shell to start the program this is so we can hide the command dos window
                                        p.StartInfo.UseShellExecute = False
                                        p.StartInfo.WorkingDirectory = strOutputFilePath
                                        ' Show no dos window if false
                                        p.StartInfo.CreateNoWindow = True
                                        p.StartInfo.RedirectStandardOutput = True
                                    
                                        Dim progBarFrm As New FormProgressBar
                                        progBarFrm.Show()
                                    end sub
                                    
                                    D Offline
                                    D Offline
                                    Dave Kreskowiak
                                    wrote on last edited by
                                    #19

                                    The reason why your UI thread blocks is not because you started the process. It's blocking because of another line AFTER your call to Start. Your executing some statement that blocks, like WaitForExit, freezing your UI. You cannot do that and expect your UI to update itself.

                                    A guide to posting questions on CodeProject[^]
                                    Dave Kreskowiak

                                    A 1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      The reason why your UI thread blocks is not because you started the process. It's blocking because of another line AFTER your call to Start. Your executing some statement that blocks, like WaitForExit, freezing your UI. You cannot do that and expect your UI to update itself.

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak

                                      A Offline
                                      A Offline
                                      Andraw Tang
                                      wrote on last edited by
                                      #20

                                      Thanks, David. After I remove calling WaitForExit() and ReadToEnd(), the progressbar works now, but the Exited event function OnProcesited is not called, so the progressbar is always shown. How can I make OnProcesited() be called?

                                      Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                                      Dim p As System.Diagnostics.Process
                                      p = sender

                                          Dim rtnStr As String = String.Empty
                                          rtnStr = p.StandardOutput.ReadToEnd()
                                      
                                          If rtnStr.Contains(" FAST terminated normally.") = True Then
                                              rtnStr = rtnStr & vbCrLf & "Please click Postprocess -> View Output File in menu to view the file."
                                              updateViewMenu()
                                          End If
                                          progBarFrm.Close()
                                          progBarFrm.Dispose()
                                      
                                          Dim frm As New FormShowFASTResult
                                          frm.txtFASTResult.Text = rtnStr
                                          frm.Button1.Select()
                                          frm.ShowDialog()
                                      
                                      End Sub
                                      
                                      
                                      Private Sub Run\_Tool()
                                      
                                          Dim sAppPath As String = ".\\XXXXX.exe"
                                          Dim fileName As String = "XXXXX.txt"
                                          Dim p As New System.Diagnostics.Process()
                                      
                                      
                                      
                                          p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                          p.StartInfo.Arguments = fileName
                                      
                                          'Do not use the system shell to start the program this is so we can hide the command dos window
                                          p.StartInfo.UseShellExecute = False
                                          p.StartInfo.WorkingDirectory = strOutputFilePath
                                          ' Show no dos window if false
                                          p.StartInfo.CreateNoWindow = True
                                          p.StartInfo.RedirectStandardOutput = True
                                          p.EnableRaisingEvents = True
                                          AddHandler p.Exited, AddressOf OnProcesited
                                      
                                      
                                          progBarFrm = New FormProgressBar
                                      
                                          Try
                                              p.Start()
                                      
                                              progBarFrm.Show()
                                              progBarFrm.Refresh()            
                                          Catch ex As Exception
                                              ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                                              
                                              Return
                                          End Try    
                                      

                                      End Sub

                                      D 1 Reply Last reply
                                      0
                                      • A Andraw Tang

                                        Thanks, David. After I remove calling WaitForExit() and ReadToEnd(), the progressbar works now, but the Exited event function OnProcesited is not called, so the progressbar is always shown. How can I make OnProcesited() be called?

                                        Private Sub OnProcesited(ByVal sender As Object, ByVal e As System.EventArgs)
                                        Dim p As System.Diagnostics.Process
                                        p = sender

                                            Dim rtnStr As String = String.Empty
                                            rtnStr = p.StandardOutput.ReadToEnd()
                                        
                                            If rtnStr.Contains(" FAST terminated normally.") = True Then
                                                rtnStr = rtnStr & vbCrLf & "Please click Postprocess -> View Output File in menu to view the file."
                                                updateViewMenu()
                                            End If
                                            progBarFrm.Close()
                                            progBarFrm.Dispose()
                                        
                                            Dim frm As New FormShowFASTResult
                                            frm.txtFASTResult.Text = rtnStr
                                            frm.Button1.Select()
                                            frm.ShowDialog()
                                        
                                        End Sub
                                        
                                        
                                        Private Sub Run\_Tool()
                                        
                                            Dim sAppPath As String = ".\\XXXXX.exe"
                                            Dim fileName As String = "XXXXX.txt"
                                            Dim p As New System.Diagnostics.Process()
                                        
                                        
                                        
                                            p.StartInfo.FileName = Application.StartupPath & "\\" & sAppPath
                                            p.StartInfo.Arguments = fileName
                                        
                                            'Do not use the system shell to start the program this is so we can hide the command dos window
                                            p.StartInfo.UseShellExecute = False
                                            p.StartInfo.WorkingDirectory = strOutputFilePath
                                            ' Show no dos window if false
                                            p.StartInfo.CreateNoWindow = True
                                            p.StartInfo.RedirectStandardOutput = True
                                            p.EnableRaisingEvents = True
                                            AddHandler p.Exited, AddressOf OnProcesited
                                        
                                        
                                            progBarFrm = New FormProgressBar
                                        
                                            Try
                                                p.Start()
                                        
                                                progBarFrm.Show()
                                                progBarFrm.Refresh()            
                                            Catch ex As Exception
                                                ShowValidationMessage("Unexception error is caught, please close and try again.", "Error")
                                                
                                                Return
                                            End Try    
                                        

                                        End Sub

                                        D Offline
                                        D Offline
                                        Dave Kreskowiak
                                        wrote on last edited by
                                        #21

                                        You can't force it. Your external process just hasn't stopped yet for some reason. Does your external process wait for the user to "Press any key to exit"?? I sure hope not!

                                        A guide to posting questions on CodeProject[^]
                                        Dave Kreskowiak

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups