NET中使用Mutex的单实例 [英] Single Instance using Mutex in NET
问题描述
如何让vb Form成为单实例使用Mutex.对于开发者!
How to make vb Form a single instance use Mutex. For the developers!
我在网上搜索如何在VB Net中而不是在c#代码中制作单实例启动表单.这是我的解决方案:
Hi, I searched in the net how to make a single instance startup form in VB Net not in c# code. Here is my solution:
- 在项目中选择添加类..."
- 重命名NativeMethods.vb"中的新类并输入以下代码:
添加此引用:Imports System.Runtime.InteropServices
Public Const HWND_BROADCAST As Integer = &HFFFF
Public Shared ReadOnly WM_SHOWME As Integer = RegisterWindowMessage("WM_SHOWME")
<DllImport("user32")>
Public Shared Function PostMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As Boolean
End Function
<DllImport("user32")>
Public Shared Function RegisterWindowMessage(ByVal message As String) As Integer
End Function
- 在项目中选择添加类..."
- 重命名main.vb"中的新类并输入以下代码:
添加此引用:Imports System.Threading
Public Sub Main()
Dim Mutex As Mutex = New Mutex(True, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}")
If Mutex.WaitOne(TimeSpan.Zero, True) Then
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
Mutex.ReleaseMutex()
Else
MessageBox.Show("Another instance of this Application is already running.", "Attention Requested", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
NativeMethods.PostMessage(CType(NativeMethods.HWND_BROADCAST, IntPtr), NativeMethods.WM_SHOWME, IntPtr.Zero, IntPtr.Zero)
End If
End Sub
- 在 Form1 上输入以下几行:
Private Const SW_HIDE As Integer = 0
Private Const SW_SHOWNORMAL As Integer = 1
Private Const SW_SHOWMINIMIZED As Integer = 2
Private Const SW_SHOWMAXIMIZED As Integer = 3
Private Const SW_SHOWNOACTIVATE As Integer = 4
Private Const SW_RESTORE As Integer = 9
Private Const SW_SHOWDEFAULT As Integer = 10
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_SYSCOMMAND As Integer = &H112
Const SC_RESTORE As Integer = &HF120
Const SC_MINIMIZE As Integer = &HF020
If m.Msg = NativeMethods.WM_SHOWME Then
ShowMe()
End If
If m.Msg = WM_SYSCOMMAND AndAlso CInt(m.WParam) = SC_RESTORE Then
End If
If m.Msg = WM_SYSCOMMAND AndAlso CInt(m.WParam) = SC_MINIMIZE Then
End If
MyBase.WndProc(m)
End Sub
Private Sub ShowMe()
If WindowState = FormWindowState.Minimized Then
WindowState = FormWindowState.Normal
End If
Show()
Dim top As Boolean = TopMost
TopMost = True
TopMost = top
'Here you can call your own function. For example if your application uses the command line!
End Sub
- 在项目中选择属性项目名称..."
- 取消勾选:启用应用程序框架"
- 在起始对象中选择:Sub Main"
- 保存项目,创建解决方案并启动它
此解决方案运行良好,并已在 Windows 7/8.1 和 10 32/64 位上进行了测试.如果有人找到更好的解决方案,请发布.感谢并致以最诚挚的问候.
This solution works very well and has been tested on Windows 7/8.1 and 10 32/64 Bit. If anyone finds a better solution, please post it. Thanks and Best Regards.
推荐答案
在WinForms项目属性中,为什么不直接勾选Make single instance application
?
In the WinForms project properties, why not just check the Make single instance application
?
这篇关于NET中使用Mutex的单实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!