小编pep*_*ero的帖子

运行一个程序实例

我有一个问题吗?!我用这种方式只运行一个程序实例.它做得非常好.但是当我在其他应用程序中使用这种方式时.当我通过桌面快捷方式运行其中一个程序时,两个程序都会在桌面上调用和显示.注意:两个程序都在windows系统中运行试试.

    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);

        }
Run Code Online (Sandbox Code Playgroud)

// ----------------主要形式

    protected override void WndProc(ref Message …
Run Code Online (Sandbox Code Playgroud)

c#

3
推荐指数
1
解决办法
2281
查看次数

标签 统计

c# ×1