G
Guest
Using VS 2003, .NET:
I developed a windows application that performs several actions based on an
input file. The application displays a progress bar as each action executes.
Based on new requirements, this application needs to be able to shell off
other processes and wait while in the mean time displaying a progress bar of
the process's. I am using the System.Disgnostics.Process class to "start"
and "waitForExit" of these processes... I thought the best way to show status
was to kick off a separate thread that increments a progress bar on my GUI
while the processes's hasExited value is false... unfortunately this is not
working well for me. As I step through the code, everything looks good until
line 11 below executes... after executing this line nothing happens and the
application just hangs. Being a novice to threads, I wasn't sure if my
application of thread was appropriate in this instance (regardless I wanted
to try and make it work as a learning exercise). So, Is my application of a
separate thread appropriate? Should it be implemented differently, etc? Any
idea why the application just hangs after line 11?
Thanks for your attention to this, Charles:
1 Public Class frmMain
2 '....snip...
3 Private oProcess As System.Diagnostics.Process
4 '....snip...
5 Friend WithEvents pgAction As System.Windows.Forms.ProgressBar
6 '....snip...
7 Public Sub New()
8 MyBase.New()
9 'This call is required by the Windows Form Designer.
10 InitializeComponent()
11 'Add any initialization after the InitializeComponent() call
13 Me.MyMain()
14 End Sub
15 '....snip...
16 Private Sub IncrementActionProgress()
17 While Not oProcess.HasExited 'HACK start with a the progress bar
moving up 2 steps / second
18 If Me.pgAction.Value = 100 Then Me.pgAction.Value = 0 'Keep
going till thread is aborted...
19 Me.pgAction.Increment(1) 'declaration not shown...
20 System.Threading.Thread.CurrentThread.Sleep(500)
21 End While
22 End Sub
23 '....snip...
24 Private Sub MyMain
25 oProcess = New System.Diagnostics.Process
26 Try
27 oProcess.StartInfo.FileName = Process
28 oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
29 oProcess.Start()
30 Dim t As System.Threading.Thread
31 t = New System.Threading.Thread(AddressOf
Me.IncrementActionProgress)
32 t.Start()
33 oProcess.WaitForExit()
34 t.Abort()
35 Me.pgAction.Value = Me.pgAction.Maximum 'Ensure that
progress bar goes to max...
36 Catch ex As Exception
37 Msgbox "Error: " & Err.Message 'TODO handle error
gracefully...
38 End Try
39 End Sub
I developed a windows application that performs several actions based on an
input file. The application displays a progress bar as each action executes.
Based on new requirements, this application needs to be able to shell off
other processes and wait while in the mean time displaying a progress bar of
the process's. I am using the System.Disgnostics.Process class to "start"
and "waitForExit" of these processes... I thought the best way to show status
was to kick off a separate thread that increments a progress bar on my GUI
while the processes's hasExited value is false... unfortunately this is not
working well for me. As I step through the code, everything looks good until
line 11 below executes... after executing this line nothing happens and the
application just hangs. Being a novice to threads, I wasn't sure if my
application of thread was appropriate in this instance (regardless I wanted
to try and make it work as a learning exercise). So, Is my application of a
separate thread appropriate? Should it be implemented differently, etc? Any
idea why the application just hangs after line 11?
Thanks for your attention to this, Charles:
1 Public Class frmMain
2 '....snip...
3 Private oProcess As System.Diagnostics.Process
4 '....snip...
5 Friend WithEvents pgAction As System.Windows.Forms.ProgressBar
6 '....snip...
7 Public Sub New()
8 MyBase.New()
9 'This call is required by the Windows Form Designer.
10 InitializeComponent()
11 'Add any initialization after the InitializeComponent() call
13 Me.MyMain()
14 End Sub
15 '....snip...
16 Private Sub IncrementActionProgress()
17 While Not oProcess.HasExited 'HACK start with a the progress bar
moving up 2 steps / second
18 If Me.pgAction.Value = 100 Then Me.pgAction.Value = 0 'Keep
going till thread is aborted...
19 Me.pgAction.Increment(1) 'declaration not shown...
20 System.Threading.Thread.CurrentThread.Sleep(500)
21 End While
22 End Sub
23 '....snip...
24 Private Sub MyMain
25 oProcess = New System.Diagnostics.Process
26 Try
27 oProcess.StartInfo.FileName = Process
28 oProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
29 oProcess.Start()
30 Dim t As System.Threading.Thread
31 t = New System.Threading.Thread(AddressOf
Me.IncrementActionProgress)
32 t.Start()
33 oProcess.WaitForExit()
34 t.Abort()
35 Me.pgAction.Value = Me.pgAction.Maximum 'Ensure that
progress bar goes to max...
36 Catch ex As Exception
37 Msgbox "Error: " & Err.Message 'TODO handle error
gracefully...
38 End Try
39 End Sub