StartupNextInstance not always firing

  • Thread starter Thread starter mjscott007
  • Start date Start date
M

mjscott007

Guest
My Windows Form app processes files and is called from the right-click context menu from Windows Explorer. The user selects one or more files, right-clicks on the selection, then picks my app from the context menu. The behaviour of Windows is to call the app once for each file in the selection, and the filename is passed as an argument. Since my app is a "single instance application", I need to use the MyApplication_StartupNextInstance event to capture all the filenames selected. It works - most of the time. Every once in a while (1 out of 5 or so), it misses a file with no error or explanation. I'll select 4 files in Win Explorer, right-click, select my program, and my program only reports on 3 of them. I get the same anomaly with both Debug and Release compiles.
The code in MyApplication_StartupNextInstance is very simple - it adds the command line param to a CheckedListBox on my main form.



Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim args As String() = Environment.GetCommandLineArgs() For i As Integer = LBound(args) To UBound(args) If i <> LBound(args) Then CheckedListBox1.Items.Add(args(i), CheckState.Checked) End If Next iEnd SubPrivate Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance For Each s As String In e.CommandLine Form1.CheckedListBox1.Items.Add(s, CheckState.Checked) NextEnd Sub


Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim args As String() = Environment.GetCommandLineArgs()
For i As Integer = LBound(args) To UBound(args)
If i <> LBound(args) Then
CheckedListBox1.Items.Add(args(i), CheckState.Checked)
End If
Next i
End Sub

Private Sub MyApplication_StartupNextInstance(sender As Object, e As StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
For Each s As String In e.CommandLine
Form1.CheckedListBox1.Items.Add(s, CheckState.Checked)
Next
End Sub

I've tried some debugging code right at the start of this Sub, and it is definitely not firing when this situation occurs.

I've also compiled my program as a multi-instance program, and it works correctly every time (I get the same numbers of instances of my app as the number of files selected in Win Explorer). From this I'm concluding that the O/S is not my issue - it is something with the Application Framework in VB.Net.

Thanks,
Mike


Continue reading...
 
Back
Top