windows防止程序重复启动

MFC(C++):

在OnInitDialog()函数中。

    CString strClassName = L"AJISWifiShareTool";
	HANDLE m_hMutex = NULL;

	m_hMutex = OpenMutex(MUTEX_ALL_ACCESS, FALSE, strClassName);
	if (m_hMutex == NULL) {
		m_hMutex = CreateMutex(NULL, TRUE, strClassName);
	}
	else {
		AfxMessageBox(csPromtRepeatOperation[g_ucLanguageIndex], MB_ICONEXCLAMATION);
		::PostMessage(this->m_hWnd, WM_CLOSE, 0, 0);
		return FALSE;
	}

C#:

static void Main()
        {
            bool createNew;

            using (Mutex m = new Mutex(true, Application.ProductName, out createNew))
            {
                if (createNew)
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new WIFIHotspot());
                }
                else
                {
                    if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == "zh-CN")
                    {
                        MessageBox.Show("程序已经运行。");
                    }
                    else if (System.Threading.Thread.CurrentThread.CurrentCulture.Name == "ja-JP")
                    {
                        MessageBox.Show("プログラムはすでに実行中です。");
                    }
                    else
                    {
                        MessageBox.Show("Program already running.");
                    }
                }
            }
                

        }

 

你可能感兴趣的:(windows,C++,C#,Windows开发)