运行程序的一个实例 [英] Run one instance of program

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

问题描述

我有一个问题呢?!
我用这种方式运行程序只有一个实例。
它的时候我用这种方式在其他应用程序做的非常好。不过。
当我通过从桌面快捷方式运行程序其中之一,这两个程序调用并显示桌面。
注:在Windows系统中运行试试这两个程序

 静态布尔OK; 
静态的Mutex互斥=新的Mutex(真的,{123Newsoft清机便携式Program123},走出OK);

///<总结>
///的主入口点应用程序。
///< /总结>
[STAThread]

静态无效的主要()
{
//Application.EnableVisualStyles();
//Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1中());

如果(mutex.WaitOne(TimeSpan.Zero,真))
{

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(假);


变种的MainForm =新Form1c();


{

mainForm.Visible = FALSE;


mainForm.WindowState = FormWindowState.Normal;

}
赶上(异常前)
{
MessageBox.Show(ex.Message,错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
}

Application.Run(MainForm的);

}
,否则
{

NativeMethods.PostMessage((IntPtr的)NativeMethods.HWND_BROADCAST,NativeMethods.WM_SHOWME,IntPtr.Zero,IntPtr.Zero) ;

}



// ---------- ------在主窗体

 保护覆盖无效的WndProc(参考消息M_C)
{

如果(M_C.Msg == NativeMethods.WM_SHOWME)
{
SHOWME();
}
base.WndProc(REF M_C);
}

// *****
私人无效SHOWME()
{

如果(==的WindowState FormWindowState.Minimized)
{
展();
的WindowState = FormWindowState.Normal;
}

//获得我们目前的最顶层的价值(我们将永远是假的,虽然)
布尔顶部=最顶层;
//使我们的形式跳转到的一切
最顶层=真顶部;
//设置回不管它是什么
=最顶层的顶;

}


解决方案

这是早已由.NET Framework的支持。你想使用WindowsFormsApplicationBase类。将IsSingleInstance属性设置为true。您可以覆盖OnStartupNextInstance方法做任何事情时,另一个实例被启动你喜欢。像恢复一审窗口。重写你的Program.cs文件看起来是这样的:

 使用系统;使用System.Windows.Forms的
;使用Microsoft.VisualBasic.ApplicationServices
; //添加引用Microsoft.VisualBasic程序

命名空间WindowsFormsApplication1 {
类项目:WindowsFormsApplicationBase {
[STAThread]
静态无效的主要(字串[] args){
VAR应用=新计划();
app.Run(参数);
}
公共计划(){
this.IsSingleInstance = TRUE;
this.EnableVisualStyles = TRUE;
this.MainForm =新Form1的();
}
保护覆盖无效OnStartupNextInstance(StartupNextInstanceEventArgs EventArgs的){
如果(this.MainForm.WindowState == FormWindowState.Minimized)this.MainForm.WindowState = FormWindowState.Normal;
this.MainForm.Activate();
}
}
}


i have one problem with this ?! i use this way for run only one instance of program. it's do very good.but when i use this way in other app . when i run one of them programs via shortcut from desktop , both programs invoke and show in desktop. note : both programs run in windows system try .

    static bool ok;
    static Mutex mutex = new Mutex(true, "{123Newsoft-Cleaner Portable Program123}",out ok);

    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]

    static void Main()
    {
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());

        if (mutex.WaitOne(TimeSpan.Zero, true))  
        {

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);


            var mainForm = new Form1c();

            try
            {                                    

                    mainForm.Visible = false;


                    mainForm.WindowState = FormWindowState.Normal;                  

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            Application.Run(mainForm);               

        }
        else
        {

            NativeMethods.PostMessage((IntPtr)NativeMethods.HWND_BROADCAST, NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero);

        }

//---------------- in main form

    protected override void WndProc(ref Message M_C)
    {

        if (M_C.Msg == NativeMethods.WM_SHOWME)
        {               
            ShowMe();
        }
        base.WndProc(ref M_C);
    }

    //*************
    private void ShowMe()
    {

        if (WindowState == FormWindowState.Minimized)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        // get our current "TopMost" value (ours will always be false though)
        bool top = TopMost;
        // make our form jump to the top of everything
        TopMost = true;
        // set it back to whatever it was
        TopMost = top;

    }  

解决方案

This is already well supported by the .NET Framework. You want to use the WindowsFormsApplicationBase class. Set the IsSingleInstance property to true. You can override the OnStartupNextInstance method to do anything you like when another instance gets started. Like restoring the window of the first instance. Rewrite your Program.cs file to look like this:

using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;   // Add reference to Microsoft.VisualBasic

namespace WindowsFormsApplication1 {
    class Program : WindowsFormsApplicationBase {
        [STAThread]
        static void Main(string[] args) {
            var app = new Program();
            app.Run(args);
        }
        public Program() {
            this.IsSingleInstance = true;
            this.EnableVisualStyles = true;
            this.MainForm = new Form1();
        }
        protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
            if (this.MainForm.WindowState == FormWindowState.Minimized) this.MainForm.WindowState = FormWindowState.Normal;
            this.MainForm.Activate();
        }
    }
}

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

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