System.Threading.Mutex

 using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace DIYShutdown
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern IntPtr FindWindow(string className, string windowText);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/DIYShutdown", out isCreated))
            {
                if (isCreated)
                {
                    Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new FormShutdown());
                    newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    IntPtr handle = FindWindow("WindowsForms10.Window.8.app.0.378734a", "DIYShutdown");
                    if (handle != IntPtr.Zero)
                    {
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                }
            }
            #endregion
        }
    }
}

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
using Microsoft.Win32;

namespace DIYShutdown
{
    static class Program
    {
        #region DllImportAttribute
        [DllImport("user32.dll", EntryPoint = "ShowWindow")]
        static extern bool ShowWindow(IntPtr handle, int flags);

        [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
        static extern bool SetForegroundWindow(IntPtr handle);
        #endregion

        [STAThread]
        static void Main()
        {
            #region Mutex
            bool isCreated; // 互斥体名称须唯一。
            using (Mutex newMutex = new Mutex(true, @"Local/DIYShutdown", out isCreated))
            {
                if (isCreated)
                {
                    Application.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        FormShutdown frame = new FormShutdown();
                        subKey.SetValue("Handle", frame.Handle);
                        Application.Run(frame);
                    }
                    newMutex.ReleaseMutex(); // 释放互斥体的所属权。
                }
                else
                {
                    string text = string.Format("“{0}”应用程序已经运行。", AppDomain.CurrentDomain.FriendlyName);
                    MessageBox.Show(text, "系统提示!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    using (RegistryKey subKey = Application.UserAppDataRegistry)
                    {
                        IntPtr handle = new IntPtr(Convert.ToInt32(subKey.GetValue("Handle")));
                        ShowWindow(handle, 1);
                        SetForegroundWindow(handle);
                    }
                }
            }
            #endregion
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值