单实例运行 [英] Single instance running

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

问题描述

我创建了一个带有2个表单的应用程序:form1和form2

如果应用程序已经在form1上运行我们只能打开一次应用程序而我按下.exe文件如果专注于当前form1已经打开问题是当打开form2并且当我按下应用程序.exe文件时,form1是隐藏它不关注form2它专注于hiden form1如何让它聚焦并激活form2如果form1是hide();



 Mutex互斥锁=新互斥锁(false,UpdateExcel); 

//实例已经运行
if(!mutex.WaitOne(0,false))
{
IntPtr winHandle = IntPtr.Zero;
Process [] localAll = Process.GetProcesses();
处理me = Process.GetCurrentProcess();

foreach(在localAll中处理p)
{

if(p.Id!= me.Id)
{
if( p.ProcessName.Equals(me.ProcessName))
{
winHandle = Process.GetProcessById((int)p.Id).MainWindowHandle;
休息;
}
}
}

if(winHandle!= IntPtr.Zero)
{
const int SW_RESTORE = 9;
if(IsIconic(winHandle)!= 0)ShowWindow(winHandle,SW_RESTORE);
SetForegroundWindow(winHandle);

}
mutex.Close();
System.Environment.Exit(0);
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());

解决方案

一个有趣的问题,我希望互斥量大师将是不久......同时...



我认为这是Windows Forms。



如果你愿意在你的C#项目中引用VisualBasic.dll,可以使用非互斥解决方案。这将使用VB.NET工具中的Microsoft.VisualBasic.ApplicationServices库,具体而言,使用WindowsFormsApplicationBase类。



Chris Sells在第14章中探讨了这一点。他的书Windows Forms 2.0编程,以及那一章应用程序......自出版以来的更新内容......可以免费下载,作为.pdf,这里:[ ^ ]。



Singleton Applications的内容从第560页开始,他包含了具体的细节和代码,用于完成......我想......你想要的......



第二个的源代码。该书的版本可在此下载为.zip文件:[ ^ ],第14章的源代码是SingleInstanceApplication的一个例子。我已经验证您可以将该代码示例转换为VS 2012并运行它,针对FrameWork 2.0或4.5进行编译。



尝试应用程序的场景运行一次,隐藏MainForm并显示第二个Form,然后双击Application的exe文件:第一个Form不会自动显示,但是,第二个Form not get焦点。



如果我有时间,今天晚些时候,如果第一个表格被隐藏,我会看看是否可以修改卖家的代码以给予第二个表格焦点。用户试图再次运行该应用程序。



免责声明:我是Addison-Wesley的技术编辑,关于Sells书的几个章节,但不是第14章

i have create an application with 2 form : form1 and form2
we can open the application only one time if the application is already running on form1 and i press on the .exe file if focus on the current form1 already opened the problem is when the form2 is opened and the form1 is hide when i press on the application .exe file it does not focus on the form2 it focus on the hiden form1 how can i make it focus and activate form2 if the form1 is hide();

Mutex mutex = new Mutex(false, "UpdateExcel");
            
            //Instance already running
            if (!mutex.WaitOne(0, false))
            {
                IntPtr winHandle = IntPtr.Zero;
                Process[] localAll = Process.GetProcesses();
                Process me = Process.GetCurrentProcess();

                foreach (Process p in localAll)
                {
                   
                    if (p.Id != me.Id) 
                    {
                            if(p.ProcessName.Equals(me.ProcessName))
                            {
                                winHandle = Process.GetProcessById((int)p.Id).MainWindowHandle;
                            break;
                            }
                    }
                }

                if (winHandle != IntPtr.Zero)
                {
                    const int SW_RESTORE = 9;
                    if (IsIconic(winHandle) != 0) ShowWindow(winHandle, SW_RESTORE);
                    SetForegroundWindow(winHandle);
               
                }
                mutex.Close();
                System.Environment.Exit(0);
            }
           Application.EnableVisualStyles();
           Application.SetCompatibleTextRenderingDefault(false);
           Application.Run(new Form1());

解决方案

An interesting question, and I hope a mutex guru will be along shortly ... meanwhile ...

I assume this is Windows Forms.

If you are willing to reference the VisualBasic.dll in your C# project, a non-mutex solution is available. This would use the Microsoft.VisualBasic.ApplicationServices library in the VB.NET facilities, and, specifically, use the WindowsFormsApplicationBase Class.

Chris Sells explored this in Chapter 14 of his book, "Windows Forms 2.0 Programming," and, that chapter, "Applications," ... with updated content since publication ... is available, free, for download, as a .pdf, here: [^].

The content on Singleton Applications begins at pp. 560, and he includes specific details, and code, for accomplishing what ... I think ... you want.

The source-code for the 2nd. edition of the book is downloadable as a .zip file here: [^], and in the source-code for Chapter 14 is an example of a SingleInstanceApplication. I've verified that you can convert that code sample to VS 2012 and run it, compiling against either FrameWork 2.0, or 4.5.

Trying out the scenario where the application is run once, and the MainForm is hidden and a second Form is shown, and then double-clicking the Application 'exe file: the first Form is not shown automatically, but, the second Form does not get focus.

If I have time, later today, I'll see if I can modify Sells' code to give the second Form focus if the first Form is hidden and the user tries to run the Application a second time.

Disclaimer: I was a technical editor for Addison-Wesley on several chapters of Sells' book, but not Chapter 14.


这篇关于单实例运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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