"make single instance application" what does this do?
Asked Answered
P

7

10

in vb 2008 express this option is available under application properties. does anyone know what is its function? does it make it so that it's impossible to open two instances at the same time?

Poetry answered 25/8, 2009 at 14:48 Comment(0)
D
-4

There is even a easier method:

Use the following code...

Imports System.IO

On the main form load event do the following:

If File.Exist(Application.StartupPath & "\abc.txt") Then
    'You can change the extension of the file to what ever you desire ex: dll, xyz etc.
    MsgBox("Only one Instance of the application is allowed!!!")
    Environment.Exit(0)
Else
    File.Create(Application.StartupPath & "\abc.txt", 10, Fileoptions.DeleteonClose)
Endif

This will take care of single instances as well as thin clients, and the file cannot be deleted while the application is running. and on closing the application or if the application crashes the file will delete itself.

Delhi answered 10/10, 2013 at 11:40 Comment(2)
Requires write-access to the directory containing the EXE, which is not available if you install your EXE under Program Files and you run the EXE as a non-admin user.Duralumin
I would not say this is easier than ticking the Make Single Instance Application checkboxHylo
O
20

does it make it so that it's impossible to open two instances at the same time?

Yes.

Obedient answered 25/8, 2009 at 14:54 Comment(0)
B
14

Why not just use a Mutex? This is what MS suggests and I have used it for many-a-years with no issues.

Public Class Form1
Private objMutex As System.Threading.Mutex
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Check to prevent running twice
    objMutex = New System.Threading.Mutex(False, "MyApplicationName")
    If objMutex.WaitOne(0, False) = False Then
        objMutex.Close()
        objMutex = Nothing
        MessageBox.Show("Another instance is already running!")
        End
    End If
    'If you get to this point it's frist instance

End Sub
End Class

When the form, in this case, closes, the mutex is released and you can open another. This works even if you app crashes.

Belvabelvedere answered 15/4, 2013 at 21:28 Comment(2)
because it's only .NET 4.5. but this is great information indeed!Poetry
Whats .NET 4.5? I have been using this since 1.1 and currently use it in VS2008, as stated in the original question.Belvabelvedere
D
11

Yes, it makes it impossible to open two instances at the same time.

However it's very important to be aware of the bugs. With some firewalls, it's impossible to open even one instance - your application crashes at startup! See this excellent article by Bill McCarthy for more details, and a technique for restricting your application to one instance. His technique for communicating the command-line argument from a second instance back to the first instance uses pipes in .NET 3.5.

Duralumin answered 25/8, 2009 at 15:34 Comment(0)
M
3
    Dim _process() As Process
    _process = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName)
    If _process.Length > 1 Then
        MsgBox("El programa ya está ejecutandose.", vbInformation)
        End
    End If
Manse answered 7/9, 2017 at 17:6 Comment(1)
Cool technique, but your post doesn't really address the OP's question. Consider moving it to a comment.Orthogonal
W
2

I found a great article for this topic: Single Instance Application in VB.NET.

Example usage:

Module ModMain

    Private m_Handler As New SingleInstanceHandler()
    ' You should download codes for SingleInstaceHandler() class from:
    ' http://www.codeproject.com/Articles/3865/Single-Instance-Application-in-VB-NET

    Private m_MainForm As Form

    Public Sub Main(ByVal args() As String)
        AddHandler m_Handler.StartUpEvent, AddressOf StartUp ' Add the StartUp callback
        m_Handler.Run(args)
    End Sub

    Public Sub StartUp(ByVal sender As Object, ByVal event_args As StartUpEventArgs)
        If event_args.NewInstance Then ' This is the first instance, create the main form and addd the child forms
            m_MainForm = New Form()
            Application.Run(m_MainForm)
        Else ' This is coming from another instance
             ' Your codes and actions for next instances...
        End If
    End Sub

End Module
Whipple answered 20/9, 2015 at 10:20 Comment(0)
J
1

Yes you're correct in that it will only allow one instance of your application to be open at a time.

Judenberg answered 25/8, 2009 at 14:56 Comment(0)
D
-4

There is even a easier method:

Use the following code...

Imports System.IO

On the main form load event do the following:

If File.Exist(Application.StartupPath & "\abc.txt") Then
    'You can change the extension of the file to what ever you desire ex: dll, xyz etc.
    MsgBox("Only one Instance of the application is allowed!!!")
    Environment.Exit(0)
Else
    File.Create(Application.StartupPath & "\abc.txt", 10, Fileoptions.DeleteonClose)
Endif

This will take care of single instances as well as thin clients, and the file cannot be deleted while the application is running. and on closing the application or if the application crashes the file will delete itself.

Delhi answered 10/10, 2013 at 11:40 Comment(2)
Requires write-access to the directory containing the EXE, which is not available if you install your EXE under Program Files and you run the EXE as a non-admin user.Duralumin
I would not say this is easier than ticking the Make Single Instance Application checkboxHylo

© 2022 - 2024 — McMap. All rights reserved.