单实例应用程序C#

单实例应用程序C#

问题描述:

我的情况是,我的应用程序首先建立与DB的连接,并使用登录程序集执行一些启动例程,然后向用户显示(另一个程序集的)主用户界面。现在我的主应用程序的另一部分向用户提供了另一个用户界面,但这也首先实例化登录程序集然后执行相同的操作。

我想要实现的是无论应用程序是什么我启动它使用登录程序集,他们只使用登录程序集的运行实例,并且不启动登录程序集的新实例。





登录程序集不是很大,但启动时所有其他应用程序必须先登录系统然后显示应用程序窗口。因此,想象一下我启动App1它将首先获取登录程序集的实例进行登录,然后启动App1窗口。然后我启动App2它做同样的事情;首先获取登录程序集的实例执行登录,然后启动App1窗口。



提到的一件事是,一旦登录成功,就会打开一个登录信息窗口,让用户始终可以获得某些信息。



因此你可以想象App1,App2,App3等都会打开一个日志信息窗口实例。



这是为什么这个想法是为每个应用程序检查是否有一个正在运行的登录信息实例,如果找到使用它,否则创建一个新的。



我是什么尝试过:



基于单一模式的登录程序集,但每个调用应用程序仍然获得登录的新实例assmelby

I have a situation where my application on first establishes a connection to the DB and does some startup routine using a login assembly and then the main user interface (of another assembly) is presented to the user. Now there is another part of my main application that presents another user interface to the user but this also first instantiate the login assembly and then does the same thing.
What I am trying to achieve is that whatever application I launch that uses login assembly they all just use the running instance of login assembly and don't launch new instance of login assembly.


The login assembly is not large but all other applications when launched have to login to the system first and then they display the application windows. So imagine I launch App1 it will first get instance of login assembly do the login and then launch the App1 window. Then I launch App2 it does the same thing; first get instance of login assembly do the login and then launch the App1 window.

One thing I missed in mentioning is that the once login is successful it leave a logon information window open to make certain information always available to the user.

Hence you can imagine App1, App2, App3 etc all leaving one instance of log information window open.

Which is why the idea is for every App to check if there is a running instance of Logon information and if found use it else create a new one.

What I have tried:

Singleton pattern based login assembly but every calling application still gets a new instance of the login assmelby

太糟糕了我目前还没有准备好发布的东西,但我有一个非常有成就的精益单实例系统,它也将命令行参数传递给原始实例,真的保证独特性,等等。也许我的简短说明可以帮到你:



输入多个只有一个流程实例的命令 [ ^ ],

自定义Windows右键单击命令启动多个实例 [ ^ ]。



如果您有任何疑问,我很乐意回答。



-SA
Too bad I don't have anything ready for publishing at the moment, but I have a very accomplished and lean single-instance system, which also passes command-line argument to an original instance, really guarantees uniqueness, and so on. Perhaps my short description can help you:

Enter multiple commands to only one process instance[^],
Custom Windows right-click command launching multiple instances[^].

If you have any questions, I'll gladly try to answer.

—SA


好的,这是我的第二个答案,更详细的介绍。我做了一些工作来概括我之前描述的解决方案,使其可以自定义并可用于各种交互式应用程序。



同时,你可以看到完整的预览源代码,完成UI演示。我希望一切都清楚: SAKryukov-SingleApplicationInstance.dotNet.src.zip(11.6 KB)



稍后,我将使用此代码撰写正式文章。如果你在我完成之前有一些问题,我会尝试回答。








在我的新文章中准备好了新的更新解决方案: 单个的所有三个功能 - One Shot的实例应用程序,.NET



重要的是,这个解决方案与我暂时发布的解决方案不同。在新版本中,我认真地关注它的可移植性。现在它应该在非Microsoft平台上正常工作(通过替代CLR实现,首先是Mono),特别是基于类UNIX 操作系统。



-SA
All right, here is my second answer, with more detail for you. I've done some work to generalize my solution I described earlier, to make it customizable and usable with all kinds of interactive applications.

In the meanwhile, you can see full preview source code, complete with UI demo. I hope everything is clear: S.A.Kryukov-SingleApplicationInstance.dotNet.src.zip (11.6 KB).

Later on, I'll write a formal article using this code. If you have some questions before I'm done, I'll try to answer.




New updated solution is ready, in my new article: All Three Features of Single-Instance Applications at One Shot, .NET.

Importantly, this solution is different from what I temporarily published. In new version, I seriously took care about its portability. Now it should work correctly (via alternative CLR implementation, first of all, Mono) on non-Microsoft platforms, in particular, based on UNIX-like OS.

—SA