如何捕获"CantStartSingleInstance"异常 [英] How to catch 'CantStartSingleInstance' Exception

查看:71
本文介绍了如何捕获"CantStartSingleInstance"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.

使用:
•MicroSoft Visual Studio 2005
•Visual Basic.NET
•NET FrameWork 3.5(但VS 2005面向FrameWork 2.0)
•Windows XP Pro SP3

我有可以读取Windows资源管理器给出的CommandLine参数的MultiMedia Player项目.程序将自身添加到Windows注册表中以进行Explorer的ContextMenu处理.
在注册表中使用参数运行我的程序的命令是:
ApplicationPath +%1"-示例:"C:\ WINDOWS \ system32 \ XSoft \ xMMPlayer.exe"%1"

当我右键单击MP3,WAV,MIDI ...文件时,将显示上下文菜单,并且有一个名为使用MM Player打开"的选项可打开程序并开始播放歌曲.

我在ApplicationEvents中的程序接收到我打开的歌曲的完整路径.

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



而且一切正常.当我通过运行或其他应用程序传递多个命令行参数时,我的程序会以以下格式接收字符串:"C:\ Media \ Song1.mp3""C:\ Media \ Song2.mp3""C:\ Media \ Song3.mp3 .
我有可以处理上述参数的函数,并且可以正常工作,将这些文件添加到PlayList并启动第一个文件.

但是有一个问题:当我在资源管理器的文件夹中选择更多文件时,然后右键单击任何选定的文件,然后单击使用MM Player打开"菜单项,将启动程序的多个实例.
好的,我制作了我的应用程序SingleInstance(在启用应用程序框架" GroupBox下的Project页面上选中该选项),并在MyApplication_StartupNextInstance事件中添加了代码.该代码类似于Startup事件中的代码.
打开我的程序并可以响应时没有问题,但是当我的程序不响应时会抛出CantStartSingleInstance异常.当然,这是在VS IDE之外发生的,并且JIT(即时调试器)显示具有上述异常的窗口.

我在程序中使用了三个Threads,一个Thread用于播放歌曲.这几乎消除了不响应"的问题,但是当我的程序没有打开并且我运行了更多的文件时,就会引发异常.我知道为什么.因为第一个实例未初始化,所以第二个实例也……并且第二个实例将参数发送给"main"或第一个实例,而第一个实例尚未初始化,然后第二个实例发送至thirth,依此类推……没有一个实例知道这是主要的(因为未初始化)并且正在互相发送...

我已经在寻找解决方案,但不幸的是,找不到.
问题是:如何捕获CantStartSingleInstance异常?"其中?
ApplicaionEvents中的事件称为UhandledException,但无法捕获异常.
我尝试过,但没有成功.



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



我请您在VB.NET或C#中给我一个示例代码,以捕获该异常或将我指向网站或可以解决我问题的内容.

更好的解决方案是为我提供代码或链接,以显示如何接收多个文件路径的一个命令行参数.
我尝试通过在注册表中将%1"替换为%L",但仍然无法正常工作.

我知道,使用ContextMenuHandler可以实现,但是直到FrameWork 4.0才建议这样做. 除了ContextMenuHandler和DDE之外,还有其他解决方案吗?据我所知,NET中不支持ContextMenuHandler和DDE?


在此先感谢您,感谢我的英语(英语)问题和长期问题.



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.

推荐答案

在资源管理器中选择多个文件并打开它们时,资源管理器将启动每个文件传递一个文件作为参数的每个文件的应用程序实例.
因此,在MyApplication_StartupNextInstance事件中,连接到已在运行的实例,然后将该新文件添加到播放列表中.为此,您可能必须在主窗口中添加一个公共功能.还可以在主窗口中添加一个属性,指示该属性是否已完全初始化-如果尚未初始化,则让新线程进入睡眠状态,然后重试.
如果要更新正在运行的应用程序的GUI,请确保调用Invoke,否则将收到一些与线程相关的异常.
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.


这篇关于如何捕获"CantStartSingleInstance"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆