我想確保我的winform應用程序只有單個實例正在運行。有沒有辦法,我能完成它通過Visual Studio 2008而不是做在後面的代碼確保只有winform應用程序的單個實例正在運行
-1
A
回答
0
靜態無效的主要() {
if (PriorProcess() != null)
{
MessageBox.Show("Another instance of the app is already running.");
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public static Process PriorProcess()
// Returns a System.Diagnostics.Process pointing to
// a pre-existing process with the same name as the
// current one, if any; or null if the current process
// is unique.
{
Process curr = Process.GetCurrentProcess();
Process[] procs = Process.GetProcessesByName(curr.ProcessName);
foreach (Process p in procs)
{
if ((p.Id != curr.Id) &&
(p.MainModule.FileName == curr.MainModule.FileName))
return p;
}
return null;
}
1
在C#中,你可以做這樣的事情在你的Program.cs
static class Program
{
public static FormMain MainForm = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE80}");
[STAThread]
static void Main()
{
if (mutex.WaitOne(TimeSpan.Zero, true))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
MainForm = new FormMain();
Application.Run(MainForm);
mutex.ReleaseMutex();
}
else
{
// send our Win32 message to make the currently running instance
// jump on top of all the other windows
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
}
}
}
,並在您的MainForm把這個任何方式
protected override void WndProc(ref Message m)
{
if (m.Msg == NativeMethods.WM_SHOWME)
{
ShowMe();
}
base.WndProc(ref m);
}
private void ShowMe()
{
if (WindowState == FormWindowState.Minimized)
{
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;
}
+0
我得到了解決方案。謝謝 –
相關問題
- 1. 如何確保只有一個iOS應用程序的實例正在運行?
- 2. 如何確保只有我的應用程序的單個實例運行?
- 3. 如何確保只有一個winform應用程序的實例C#
- 4. 如何確保Silverlight OOB應用程序只有一個實例?
- 5. 確保只有一個應用程序實例
- 6. 如何運行c#WinForm應用程序的一個實例?
- 7. 如何運行只有一個應用程序的實例
- 8. 似乎只有一個網絡應用程序實例正在運行
- 9. 在單實例應用程序中顯示正在運行的實例
- 10. 確保使用WINAPI的應用程序的單個實例?
- 11. 確保只有一個類的實例運行
- 12. 只允許一個程序正在運行的實例
- 13. 節點集羣:確保一次只有一個函數實例正在運行
- 14. 運行Winform應用程序
- 15. 如何確保只有一個線程的實例在Python中運行?
- 16. 如何確保只有單個進程在線程中運行
- 17. 確保只有一個Perl程序實例正在運行的最佳方式是什麼?
- 18. 在運行的roboguice應用程序中替換單例實例
- 19. 如何確保只有一個守護進程正在運行?
- 20. 使用Mutex運行應用程序的單個實例
- 21. 在Android的菜單隻有每個應用程序運行
- 22. 確保RMI只在一個服務器實例上運行
- 23. 從一個實例運行另一個應用程序實例。
- 24. 確保正在運行一個applescript的實例
- 25. 使用Applescript有選擇地激活一個應用程序的實例與兩個正在運行的實例
- 26. 確保單個課程實例的正確方法是什麼?
- 27. WinForm應用程序不能正確
- 28. 用一個新的實例代替正在運行的程序
- 29. 從matlab運行單個應用程序實例
- 30. 確保單個任務實例正在運行的分佈式工作人員
它是VB.NET的一項內置功能。如果你使用另一種語言,那麼你必須寫幾行代碼。 –