两次运行Winform应用程序 [英] operate winform application twice

查看:84
本文介绍了两次运行Winform应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我使用互斥锁操作的winforms [c#]开发了应用程序,因此一次只能操作一次.
最近,有必要允许该程序具有另一个实例-为了其他目的,而与第一个实例没有任何联系.

因此,我不想删除该互斥锁,因为我不想允许用户打开它100次(例如记事本),并且还可以防止在用户没有注意到该程序已经在工作时出错.
我还尝试将我的项目名称更改为另一个名称,以使程序可以像其他名称一样工作.我还通过更改程序集的默认名称(在解决方案属性中)更改了exe文件名,
但是更改效果不佳,并且我仍然存在很多引用错误.

我会为您能想到的每一个想法而感激.
谢谢!
Tali.

Hi I have developed application using winforms [c#], operated by mutex, so that it can be operated only once at a time.
Lately it became necessary to allow this program to have another instance - for other purposes, with no connection to the first instance, of cource.

So, i don''t want to remove the mutex since i don''t want to allow users open it 100 times [like notepad] and also to prevent mistakes when user doesn''t notice that the program already in work..
I also tried to change my project name to another in order to make the program work as a different one, and i also changed the exe file name by changing its assembly default name [in solution properties],
but the change didn''t work well and i still had many refference errors.

I''d be thankful for every idea you can think about.
Thanks !
Tali.

推荐答案

因此将其更改为允许两个实例,或者只是删除互斥锁并忽略非问题.
So change it to allow two instances, or just remove the mutex and ignore the non-issue.


只需删除单实例限制并完成它即可.为什么我们将应用程序限制为单个实例?因为有道理,所以并没有削减它.将应用程序限制为单个实例的原因是有限的,但是这样做的请求无处不在.那告诉我它被过度使用了.

您真的关心应用程序是否启动了100次?帮自己一个忙:消除约束,并在其他地方花费编程时间.
Just remove the single instance limitation and be done with it. Why do we limit applications to single instance? Because it makes sense doesn''t cut it. The reasons to limit an application to a single instance are finite, but the requests to do so are ubiquitous. That tells me it''s being overused.

Do you really care if the application is started 100 times? Do yourself a favor: remove the constraint and spend your programming time elsewhere.


此代码很可能设计为将实例数保持为0到1.更确切地说,第二个加载实例运行了很短的时间,直到发现第一个实例已经在运行,然后立即终止.如果删除此代码,则可以多次加载您的应用程序,但不能重复输入该数字,例如3或4.如果不看代码,很难说出如何修改它.而且很可能没有任何意义.

您所发生的情况是使用其他人的代码而不了解其工作原理的通常结果.然后,您尝试使用尝试结束错误的盲目方法来修改行为.这不是实现这些目标的方法.

最有可能的是,您将需要编写全新的代码来实现所需的功能.但是首先考虑您是否真的需要它,为什么.有多种方法可以在运行时限制应用程序实例的数量.一种简单的方法是:在您的应用程序中,使用当前进程的名称运行System.Diagnostics.Process.GetProcessesByName(string).某些此类进程的名称可能是巧合的,因此请检查每个进程的Process.MainModuleSystem.Diagnostics.ProcessModule.FileName以确保它是从相同的可执行文件加载的,与您的入口程序集的主要可执行模块相同(在您的情况下).

根据此信息,计算运行您的应用程序的单独实例的进程总数,并确定是否要运行另一个实例.如果不是,请立即终止当前过程.

参见:
http://msdn.microsoft.com/en-us/library/system.diagnostics. process.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system.diagnostics. processmodule.aspx [ ^ ].

—SA
Most likely, this code is designed to keep number of instances 0 to 1; more exactly, second instance of loading runs for a short time until it discovers that the first instance is already running, to terminate immediately. If you remove this code, you will be able load your application any number of times, but you cannot make this number, say, 3 or 4 and no more. Without looking at the code, it''s hard to say how to modify it; and most likely it would not make any sense.

What happens with you is the usual consequence of using some other person''s code without understanding how it works. And then you try to modify behavior using trial-end-error, blind-folded approach. This is not how such things can be achieved.

Most likely, you will need to write a brand new code to achieve what you want. But first think if you really need it and why. There is a number of ways to limit number of application instances during run time. One simple approach is this: in your application, run System.Diagnostics.Process.GetProcessesByName(string) using the name of current process. Some of such processes can have the same name by coincidence, so examine Process.MainModule for each process and System.Diagnostics.ProcessModule.FileName to make sure it is loaded from the same executable file, in your case, same as main executable module of your entry assembly.

Based on this information, calculate the total number of processes running a separate instance of your application and decide if you want to run another one. If not, terminate your current process immediately.

See:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[^],
http://msdn.microsoft.com/en-us/library/system.diagnostics.processmodule.aspx[^].

—SA


这篇关于两次运行Winform应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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