Hello everyone.
Using:
• MicroSoft Visual Studio 2005
• Visual Basic.NET
• NET FrameWork 3.5 (but VS 2005 is targeting FrameWork 2.0)
• Windows XP Pro SP3
I have MultiMedia Player project that can read
CommandLine
arguments given by Windows Explorer. Program adds itself in Windows Registry for Explorer's ContextMenu handling.
The command in Registry to run my program with argument is:
ApplicationPath + " %1" - example: "C:\WINDOWS\system32\XSoft\xMMPlayer.exe" "%1"
When I right click on MP3, WAV, MIDI ... files, context menu is shown and there is option called 'Open with MM Player' that opens my program and starts song playing.
My program in
ApplicationEvents
receives full path of the song I opened.
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
Select Case e.CommandLine.Count
Case 0
Case 1
ProcessSingleCMD_ARGS(e.CommandLine)
Case Else
ProcessMultipleCMD_ARGS(e.CommandLine)
End Select
End Sub
End Class
And all is working fine. When I pass multiple command line arguments via Run or another application, my program receives string in next format: "C:\Media\Song1.mp3" "C:\Media\Song2.mp3" "C:\Media\Song3.mp3".
I have function that can handle arguments like above, and that is working, these files are added to PlayList and first one is started.
But there is a problem: when I select more files in Explorer's folder, then right click on any of selected, and click on 'Open with MM Player' menu item, multiple instances of my program are started.
OK, I made my application
SingleInstance
(checking that option on Project's page under 'Enable Application Framework' GroupBox), and added code in
MyApplication_StartupNextInstance
event. That code is similar to code in
Startup
event.
There is no problem when my program is opened and can respond, but when my program is not responding
CantStartSingleInstance
exception is thrown. Of course this is happening outside VS IDE, and JIT (Just In Time debugger) shows window with above mentioned exception.
I'm using three
Threads
in my program and one
Thread
is for playing songs. That almost eliminates 'not responding' problem, but when my program is not opened and I run more files, then exception is thrown. I know why. Because first instance is not initialized, second too ... and second instance send arguments to 'main' or first instance and first instance is not initialized yet, then second instance send to thirth and so on ... No one instance knows which is main (because is not initialized) and are sending to each other ...
I have searched for solution but, unfortunately, not found.
The question is:
'How to catch CantStartSingleInstance exception ?' Where ?
In
ApplicaionEvents
is event called
UhandledException
but can't catch exception.
I tried with this, but with no success.
If TypeOf (e.Exception) Is CantStartSingleInstanceException Then
MessageBox.Show("CantStartSingleInstanceException was thrown")
e.ExitApplication = True
End If
I please You to give me a sample code in VB.NET or C# to catch that exception or point me to web site or something that can solve my problem.
Better solution would be to give me code or link that shows how to receive one command line argument of multiple file paths.
I tried by replacing '%1' with '%L' in Registry, but still not working.
I know, that is possible with ContextMenuHandler, but that is not recommended until FrameWork 4.0.
Is there any other solution, except ContextMenuHandler and DDE which is not supported in NET (as far I know) ?
Thanks in advance, and sorry for my (poor) english and long question.