不懂为什么不能打开一个程序两次?

c#中不能同时打开一个程序两次
把这个程序复制到另外一个地方,然后重命名,还是不能打开
求教!谢谢

温温酱
浏览 109回答 2
2回答

幕布斯6054654

你的意思应该是不能同时打开一个程序的两个进程吧;C#默认都是允许打开多个的,除非你自己进行了限制,一般的限制代码都在Program中的Main方法中,用来检查系统中是否已存在此程序的进程,你可以和新建的带Main的方法进行一下比较,把不一样(一般用到System.Diagnostics.Process类)的代码删除或者注释

绝地无双

两个主流的方法。方法一:使用Mutex来进行1. 首先要添加如下的namespace:using System.Threading;2. 修改系统Main函数,大致如下:bool bCreatedNew;//Create a new mutex using specific mutex nameMutex m =new Mutex( false, "myUniqueName", out bCreatedNew );if( bCreatedNew )Application.Run(new yourFormName());如上面编码就可以了,要注意的一点是,在给Mutex起名字的时候,不要太简单,以防止和其他程序的Mutex重复,从而达不到所预想的效果。方法二:使用Process来进行1. 首先要添加如下的namespace:using System.Diagnostics;using System.Reflection;2. 添加如下函数:public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } 3. 修改系统Main函数,大致如下:if( RunningInstance() == null )Application.Run(new yourFormName());如上面编码就可以了,要注意的一点是,在判断进程模块文件名是否相等这部分的代码,是可选的。如果当前的程序在文件系统中只存在一个的话,以上的方法是可以的;否则不要删除这部分的代码。对比两种方法,就效率和简便性来说,前一种方法是最好的,也是我比较喜欢的;后一种方法,速度比较慢,其次通过ProcessName去系统中查寻,有可能查出来的Process并不是我想要得,虽说在后面加了文件目录判断,但是其含有潜在的问题(前面已经说出来)。不过,第一种方法也有缺陷,就是扩展性操作不方便,例如:让程序只运行一次,如果程序已经运行,把它弹出并显示到最前面。对于此,后一种方法就很有优势了。 
打开App,查看更多内容
随时随地看视频慕课网APP