[FONT=Courier, Monospaced]Hi,
I have created a vb.net windows application. I finished creating setup
project and installtion. Now, i realized that i want to include this
option to my application, like, when the application is already running
in the desktop(minimised), another same application shouldn't be
opened. For this i used this code:
Inside Class:
-------------------------
Public Sub New()
MyBase.New()
If InstanceRunning(False) = True Then
Dim ans As String = MsgBox("The application is already
running in the desktop")
Application.Exit()
Application.Run()
End If
GC.KeepAlive(oMutex)
End Sub
Private oMutex As Mutex
Private Function InstanceRunning(ByVal UserContex As Boolean) As
Boolean
Dim progid As String = "{4C91B499-109B-42c8-B68E-E125F321EED6}"
Dim createdNew As Boolean
oMutex = New Mutex(False, String.Concat(progid,
CStr(IIf(UserContex, System.Environment.UserName, ""))))
If oMutex.WaitOne(0, False) = False Then
oMutex.Close()
Return True
End
End If
End Function
----------------------------------------------------
This is working perfect. If i try to open a second application, it will
give the message that "its alreday opened". But, what i want is , after
this error message, i want my first application(thats minimised in the
desktop) to get popped up infront of the user in the screen.
I tried some possibilities like:
Me.WindowState = FormWindowState.Maximized
Me.ActiveForm.Show()
.focus()
etc..etc...
but, nothing is working...
i saw something like the following code from net
----------------------------------------------------------------------------------------:
Public Enum ShowWindowConstants
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum
<DllImport("User32.dll")> _
Public Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal
swCommand As Integer) As Integer
End Function
<STAThread()> _
Public Shared Sub Main()
Dim RunningProcesses As Process() =
Process.GetProcessesByName("SingletonApp")
'Change the name above to suit your application
If (RunningProcesses.Length = 1) Then
Application.Run(New Form1)
Else
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_SHOWMINIMIZED)
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_RESTORE)
End If
End Sub
---------------------------------------------------------
But, this is not working..
Its telling error with the line:
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_SHOWMINIMIZED)
how to set it right?
or is there any other way to maximize my first application??
thnx,
Priya
[/FONT]
I have created a vb.net windows application. I finished creating setup
project and installtion. Now, i realized that i want to include this
option to my application, like, when the application is already running
in the desktop(minimised), another same application shouldn't be
opened. For this i used this code:
Inside Class:
-------------------------
Public Sub New()
MyBase.New()
If InstanceRunning(False) = True Then
Dim ans As String = MsgBox("The application is already
running in the desktop")
Application.Exit()
Application.Run()
End If
GC.KeepAlive(oMutex)
End Sub
Private oMutex As Mutex
Private Function InstanceRunning(ByVal UserContex As Boolean) As
Boolean
Dim progid As String = "{4C91B499-109B-42c8-B68E-E125F321EED6}"
Dim createdNew As Boolean
oMutex = New Mutex(False, String.Concat(progid,
CStr(IIf(UserContex, System.Environment.UserName, ""))))
If oMutex.WaitOne(0, False) = False Then
oMutex.Close()
Return True
End
End If
End Function
----------------------------------------------------
This is working perfect. If i try to open a second application, it will
give the message that "its alreday opened". But, what i want is , after
this error message, i want my first application(thats minimised in the
desktop) to get popped up infront of the user in the screen.
I tried some possibilities like:
Me.WindowState = FormWindowState.Maximized
Me.ActiveForm.Show()
.focus()
etc..etc...
but, nothing is working...
i saw something like the following code from net
----------------------------------------------------------------------------------------:
Public Enum ShowWindowConstants
SW_HIDE = 0
SW_SHOWNORMAL = 1
SW_NORMAL = 1
SW_SHOWMINIMIZED = 2
SW_SHOWMAXIMIZED = 3
SW_MAXIMIZE = 3
SW_SHOWNOACTIVATE = 4
SW_SHOW = 5
SW_MINIMIZE = 6
SW_SHOWMINNOACTIVE = 7
SW_SHOWNA = 8
SW_RESTORE = 9
SW_SHOWDEFAULT = 10
SW_FORCEMINIMIZE = 11
SW_MAX = 11
End Enum
<DllImport("User32.dll")> _
Public Shared Function ShowWindowAsync(ByVal hWnd As IntPtr, ByVal
swCommand As Integer) As Integer
End Function
<STAThread()> _
Public Shared Sub Main()
Dim RunningProcesses As Process() =
Process.GetProcessesByName("SingletonApp")
'Change the name above to suit your application
If (RunningProcesses.Length = 1) Then
Application.Run(New Form1)
Else
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_SHOWMINIMIZED)
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_RESTORE)
End If
End Sub
---------------------------------------------------------
But, this is not working..
Its telling error with the line:
ShowWindowAsync(RunningProcesses(0).MainWindowHandle,
ShowWindowConstants.SW_SHOWMINIMIZED)
how to set it right?
or is there any other way to maximize my first application??
thnx,
Priya
[/FONT]