Execute Application

victorsj

New member
Joined
Nov 7, 2005
Messages
2
Programming Experience
1-3
Since I can make to prevent that an application is executed but of once

Thanks.
 
I'm going to assume you're asking "how can I make an application run once?"

Create a new kill process. Right before the original application (the one you want to destroy) is finished, start a new single "while true" process attempting to kill the .exe. It won't work the first time (you will probably get an application in use error), but let the process repeat until it can.

There's probably cleaner, non-abussive ways of doing it. This is just how I like to handle it. :)
 
VB.NET:
Form's Load Event:
  Dim blnPrevApp As Boolean = PrevInstance()
  If blnPrevApp = True Then Me.Close()

 Friend Function PrevInstance() As Boolean
   If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
     Return True
   Else
     Return False
   End If
 End Function

what this does is right when you're app loads, it checks to see if an instance is already running, if it is then you close the one that just opened
 
Thanks juggalo, I had a more execute and execute approach :)

I was trying to make sure it couldn't run again. You're probably closer to what he wanted.
 
I would suggest a similar approach but use a Main method rather than a form's Load event. That way you never even create a form if you aren't going to run the app:
VB.NET:
Public Shared Sub Main()
    If Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Length > 0 Then
        Application.Run(New MainForm)
    End If
End Sub
That way no form is ever created if the app is already running and the new process simply exits gracefully.
 
Can you explain "Public Shared Sub Main"...

is main running all the time within the exe or something so you can call it anytime without importing the namespace etc.
 
For a single App form, the Sub Main procedure is created for you in the background by the compiler (this is done because VB is intended to be used for Rapid Application Development). If your single form were named Form1, a procedure similar to the following would be created and executed:
VB.NET:
<STAThread()> Public Shared Sub Main()
    Dim frm As New Form1()
    frm.show()
End Sub
You can however create your own entry point (in some Module or Class file) and use it as the Startup Object (in the Property pages, set the StartUp Object to 'Sub Main' in the dropDown provided under Common Properties > General).
 
Just note Paszt that Form.Show returns immediately so your Main method would complete straight away and the app would exit. It would be more like Application.Run(frm) instead. Not that it really matters, but for interest's sake you don't actually have to set the startup object to the Main method. If you set the startup object to be a form or module that contains a Main method then it will automatically be used as the entry point. I guess the only advantage of that would be that you can then have Main methods in more than one place, which sounds like a bad idea anyway. Interesting though, don't you think? Well, do ya? :)
 
Back
Top