VB.NET Progress status while run a EXE file
-
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!
-
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!
You can show the progress bar all you want. You don't haven't shown any code that updates the progress bar at all. p.Start is not a blocking call, so your process will launch and run independent of this code.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
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!
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.
-
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.
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()
-
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.
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
-
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
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.
-
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.
Thanks for reply, I modify the code, but still don't work
-
Thanks for reply, I modify the code, but still don't work
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.
-
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.
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
-
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
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.
-
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
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 -
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
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.
-
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.
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 -
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.
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
-
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 KreskowiakProgressbar 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.
-
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 KreskowiakSorry 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
-
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 KreskowiakIf 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
-
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
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 = stDim 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 ClassAlso, 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 -
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
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 -
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 KreskowiakThanks, 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 = senderDim 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