Click here to Skip to main content
14,542,223 members
Rate this:
Please Sign up or sign in to vote.
See more:
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
                'StartNormal
            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.
Posted
Comments
ledtech3 4-Jun-12 21:36pm
   
try adding a try catch
Try
Your Code here
Catch ex As Exception
MsgBox(ex.Message)
End Try
XSON-NEON 5-Jun-12 12:21pm
   
Thank you very much for answer, but code you posted will not catch CantStartSingleInstance exception. Unfortunately that is truth and I already have code like yours and is not working.

Anyways thanks.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

When you select multiple files in explorer and open them, explorer starts an instance of your application for each of the files passing one file per applciation as an argument.
Consequently, in the MyApplication_StartupNextInstance event, connect to the already running instance, and add that new file to the play list; you may have to add a public function to your main window for that purpose. Also add a property to your main window indicating whether it is fully initialized or not - if it is not yet initialized just let the new thread sleep and try again.
If you want to update the GUI of the running application, make sure to call Invoke as otherwise you'll receive some threading related exceptions.
   
Comments
XSON-NEON 5-Jun-12 12:32pm
   
Thanks for Your solution. I will try with adding function to check is fully initialized. I think problem is because every application is sending arguments to each other, and all aplications are receiving arguments in StartupNextInstance Event. Because of that, exception is thrown. On my second computer (which is 7x faster than my current) I can run about 35-40 songs without error. Sometimes exception is thrown, sometimes not. That depends of number of programs I'm running ... but sometimes on faster computer that exception is thrown for only two files ... very confusing ...

I'll try to solve that with solution You posted and thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100