How to make only one instance of a program to run?

sonasivas

Member
Joined
Apr 11, 2006
Messages
5
Programming Experience
1-3
[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]
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
I see your platform is .Net 2.0, so... in application settings just enable the checkbox "Make single instance application"
 

sonasivas

Member
Joined
Apr 11, 2006
Messages
5
Programming Experience
1-3
Hi John,

Thankyou for the reply. How can i do the settings that mentioned by u?
I was searching for the application settings in my properties window, but i couldn't find out... Where to find it?

tnx,
Priya
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,609
Location
Norway
Programming Experience
10+
Main Project menu, Application Properties choice, Application Tab, one of the checkboxes there. Or context menu on Solution Explorer.
 

sonasivas

Member
Joined
Apr 11, 2006
Messages
5
Programming Experience
1-3
Now i've found out the reason why couldnt i do anything what you've mentioned. Its because i'm not using .Net 2.0, but .Net 1.1.

And, the most important one is I HAVE NO PROBLEM IN MAKING ONLY ONE APPLICATION TO RUN, I CAN DO THAT, MY ACTUAL PROBLEM IS I COULDNT POP-UP MY FIRST APPLICATION TO THE SCREEN.

Anyone help me ???

tnx..
priya
 
Top Bottom