Running one Instance of EXE
-
I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent
-
I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent
Use a Mutex - so you are going to have to look at the Win32 API. Have a look at CreateMutex - to make a named Mutex. You can only create a Mutex once across processes, so the if a second instance launches and tries to create it, there will be an error, which can be caught. So you would put this routine in the start up code.
Shameless Plug - Distributed Database Transactions in .NET using COM+
-
I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent
Interesting questions! For the single app instance, try using a Mutex: Sub Main() Dim M1 As System.Threading.Mutex Dim createdNew As Boolean = False M1 = New System.Threading.Mutex(True, "MyUniqueMutexName", createdNew) If createdNew = False Then MsgBox("Other process is running") Return End If Application.Run(New Form1()) End Sub For single form intance, this is a bit of a hack, but it works. The problem with maintaining a member-level ref to a form occurs after the form has been closed. At this point the form is disposed, but we have no way of determining the disposed state by examining the ref (that I know of). My hack catches an ObjectDisposedException as a way of determining the disposed state. You could also subclass Form, override Dispose(bool) and provide an IsDisposed property. ' Member-level reference Private _Form2 As Form2 ... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If _Form2 Is Nothing Then _Form2 = New Form2() Try _Form2.Show() _Form2.BringToFront() Catch ex As ObjectDisposedException _Form2 = New Form2() _Form2.Show() End Try End Sub
-
Use a Mutex - so you are going to have to look at the Win32 API. Have a look at CreateMutex - to make a named Mutex. You can only create a Mutex once across processes, so the if a second instance launches and tries to create it, there will be an error, which can be caught. So you would put this routine in the start up code.
Shameless Plug - Distributed Database Transactions in .NET using COM+
-
I have a VB.net Application ,whenever i click on the EXE a new instance of the application runs .But i want one instance to run at a time .That means if the exe is already running by clicking on it it should show the message that it is already running. Similar case for loading forms.suppose i have a button ,clicking on it a form is comes up ,if i click on the button several instance of the same form is running ,how to get rid of this that means there must be one instance of a form at a time. Please help me its urgent
-
See my reply to the original message. There is a System.Threading.Mutex class available in the Fx.
Oops, did not see that, I thought he was talking ordinary VB for a moment. Anyway Mutex would seem the way to go, and its much easier in .NET. :)
Shameless Plug - Distributed Database Transactions in .NET using COM+
-
Interesting questions! For the single app instance, try using a Mutex: Sub Main() Dim M1 As System.Threading.Mutex Dim createdNew As Boolean = False M1 = New System.Threading.Mutex(True, "MyUniqueMutexName", createdNew) If createdNew = False Then MsgBox("Other process is running") Return End If Application.Run(New Form1()) End Sub For single form intance, this is a bit of a hack, but it works. The problem with maintaining a member-level ref to a form occurs after the form has been closed. At this point the form is disposed, but we have no way of determining the disposed state by examining the ref (that I know of). My hack catches an ObjectDisposedException as a way of determining the disposed state. You could also subclass Form, override Dispose(bool) and provide an IsDisposed property. ' Member-level reference Private _Form2 As Form2 ... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If _Form2 Is Nothing Then _Form2 = New Form2() Try _Form2.Show() _Form2.BringToFront() Catch ex As ObjectDisposedException _Form2 = New Form2() _Form2.Show() End Try End Sub