EDN Admin
Well-known member
Using a technique similar to that described at msdn.microsoft.com/en-us/library/stws2269(v=vs.100).aspx, I created my utility program to run in "batch" mode without opening any windows. It works great. But now I need my utility to run in either
batch mode or interactive mode. So if the /batch switch is not on the command line, I want to display a progressbar along with a Cancel button. I have made a form containing a progressbar control, a button control, and a label control. The
progressbar and the label work fine. But I cant get the Cancel button to work. When I click on the button as a file is processing, the Click event is not sensed. I have a Messagebox inserted into the Click Event method, but when I click
the button once during the processing of a file, nothing happens. If I click two or three times, the whole window in which the progress bar and the button are in goes white and I get "Not Responding" in the title bar. But behind the scenes, the
file is still processing and when it finishes, the window closes and I have a processed file. All the while, my mouse cursor is an hourglass. In debug mode, the code in the Click Event method is never executed.
I tried adding a runtime event handler as described at msdn.microsoft.com/en-us/library/dfty2w4e.aspx<br/>
but that didnt work either. Behavior is the same.
Ive read up on delegates, but if Im understanding them correctly, they would not be applicable to this situation: Im not trying to raise a data-driven event.
Why isnt the underlying runtime sensing my button click event?
<pre class="prettyprint lang-vb Contents of ApplicationEvents.vb:
Namespace My
The following events are available for MyApplication:
Startup: Raised when the application starts, before the startup form is created.
Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
UnhandledException: Raised if the application encounters an unhandled exception.
StartupNextInstance: Raised when launching a single-instance application and the application is already active.
NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
This example uses the My.Application.Startup event to Cancel
the Form.
Private Sub MyApplication_Startup(
ByVal sender As Object,
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
) Handles Me.Startup
e.Cancel = True
Dim c As New BatchApplication
c.Main()
End Sub
End Class
End Namespace
Contents of BatchApplication.vb:
Imports System.Windows
Imports System.IO
Imports Form1.Form1
Public Class BatchApplication
Public Shared bCancel
Private bBatch As Boolean
Dim pcDone As Single
Dim bytesWritten, bytesTotal As Integer
Dim bProgressShown As Boolean
Dim rc As DialogResult
Sub Main()
GetArgs()
Determine if process was interactively or batch invoked
If Len(strBatch) = 0 Then
bBatch = False
ElseIf strBatch = "False" Or strBatch = "0" Then
bBatch = False
Else
bBatch = True
End If
bCancel = False
Dim objReader As StreamReader = New StreamReader(strIn)
strOut = Path.GetTempFileName()
Dim objWriter As StreamWriter = New StreamWriter(strOut)
Dim objFInfo As New FileInfo(strIn)
bytesTotal = objFInfo.Length
bytesWritten = 0
bProgressShown = False
If Not bBatch Then
pcDone = 0
Form1.Show()
Form1.btnCancel.Update() required to make the button display on the screen
Form1.lblPhase.Update() required to make the label display on the screen
Form1.btnCancel.Focus() added this but doesnt seem to help
bProgressShown = True
End If
Simple read/write loop until end of file...
While Not objReader.EndOfStream
i = objReader.Read
objWriter.Write(Chr(i))
If bProgressShown Then
If bCancel Then
messagebox is temporary so I know this works. Normally I would just call Abort...
rc = MessageBox.Show("Cancel button hit.", "", MessageBoxButtons.OKCancel)
If rc = DialogResult.Cancel Then Abort()
End If
bytesWritten += 1
If CType(bytesWritten / bytesTotal, Single) > pcDone Then
This graphically advances the progress bar on the screen...
Form1.pgbProgress.Value += 1
pcDone = CType(Form1.pgbProgress.Value / Form1.pgbProgress.Maximum, Single)
End If
End If
End While
End Sub
Sub Abort()
Rollback all data; delete temp files, etc.
Environment.Exit(1)
End Sub
End Class
Contents of Form1.vb:
Public Class Form1
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
messagebox is temporary so I know this works.
MessageBox.Show("Ive clicked the Cancel button")
BatchApplication.bCancel = True
End Sub
End Class[/code]
<br/>
View the full article
batch mode or interactive mode. So if the /batch switch is not on the command line, I want to display a progressbar along with a Cancel button. I have made a form containing a progressbar control, a button control, and a label control. The
progressbar and the label work fine. But I cant get the Cancel button to work. When I click on the button as a file is processing, the Click event is not sensed. I have a Messagebox inserted into the Click Event method, but when I click
the button once during the processing of a file, nothing happens. If I click two or three times, the whole window in which the progress bar and the button are in goes white and I get "Not Responding" in the title bar. But behind the scenes, the
file is still processing and when it finishes, the window closes and I have a processed file. All the while, my mouse cursor is an hourglass. In debug mode, the code in the Click Event method is never executed.
I tried adding a runtime event handler as described at msdn.microsoft.com/en-us/library/dfty2w4e.aspx<br/>
but that didnt work either. Behavior is the same.
Ive read up on delegates, but if Im understanding them correctly, they would not be applicable to this situation: Im not trying to raise a data-driven event.
Why isnt the underlying runtime sensing my button click event?
<pre class="prettyprint lang-vb Contents of ApplicationEvents.vb:
Namespace My
The following events are available for MyApplication:
Startup: Raised when the application starts, before the startup form is created.
Shutdown: Raised after all application forms are closed. This event is not raised if the application terminates abnormally.
UnhandledException: Raised if the application encounters an unhandled exception.
StartupNextInstance: Raised when launching a single-instance application and the application is already active.
NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
Partial Friend Class MyApplication
This example uses the My.Application.Startup event to Cancel
the Form.
Private Sub MyApplication_Startup(
ByVal sender As Object,
ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs
) Handles Me.Startup
e.Cancel = True
Dim c As New BatchApplication
c.Main()
End Sub
End Class
End Namespace
Contents of BatchApplication.vb:
Imports System.Windows
Imports System.IO
Imports Form1.Form1
Public Class BatchApplication
Public Shared bCancel
Private bBatch As Boolean
Dim pcDone As Single
Dim bytesWritten, bytesTotal As Integer
Dim bProgressShown As Boolean
Dim rc As DialogResult
Sub Main()
GetArgs()
Determine if process was interactively or batch invoked
If Len(strBatch) = 0 Then
bBatch = False
ElseIf strBatch = "False" Or strBatch = "0" Then
bBatch = False
Else
bBatch = True
End If
bCancel = False
Dim objReader As StreamReader = New StreamReader(strIn)
strOut = Path.GetTempFileName()
Dim objWriter As StreamWriter = New StreamWriter(strOut)
Dim objFInfo As New FileInfo(strIn)
bytesTotal = objFInfo.Length
bytesWritten = 0
bProgressShown = False
If Not bBatch Then
pcDone = 0
Form1.Show()
Form1.btnCancel.Update() required to make the button display on the screen
Form1.lblPhase.Update() required to make the label display on the screen
Form1.btnCancel.Focus() added this but doesnt seem to help
bProgressShown = True
End If
Simple read/write loop until end of file...
While Not objReader.EndOfStream
i = objReader.Read
objWriter.Write(Chr(i))
If bProgressShown Then
If bCancel Then
messagebox is temporary so I know this works. Normally I would just call Abort...
rc = MessageBox.Show("Cancel button hit.", "", MessageBoxButtons.OKCancel)
If rc = DialogResult.Cancel Then Abort()
End If
bytesWritten += 1
If CType(bytesWritten / bytesTotal, Single) > pcDone Then
This graphically advances the progress bar on the screen...
Form1.pgbProgress.Value += 1
pcDone = CType(Form1.pgbProgress.Value / Form1.pgbProgress.Maximum, Single)
End If
End If
End While
End Sub
Sub Abort()
Rollback all data; delete temp files, etc.
Environment.Exit(1)
End Sub
End Class
Contents of Form1.vb:
Public Class Form1
Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
messagebox is temporary so I know this works.
MessageBox.Show("Ive clicked the Cancel button")
BatchApplication.bCancel = True
End Sub
End Class[/code]
<br/>
View the full article