How to check that only one instance (pro user) of my application is running?

Try this function

Code:
'Checks to see if application is already running
    Function PrevInstance() As Boolean
        Try
            If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
        End Try
    End Function
 
Checking for the process by name isn't an atomic operation and as such if two instances where launched very close together it is possible for the second to not detect the first.

Joe Mamma's suggestion of using a mutex is more reliable, quick sample
Visual Basic:
    Dim m As Threading.Mutex

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b As Boolean
        m = New Threading.Mutex(True, "Test", b)

        If b = True Then
            'already running
        Else
            'guess what - not running
        End If


    End Sub
 
Thanx, I've read a little bit about mutex class:
http://msdn.microsoft.com/library/d.../html/frlrfSystemThreadingMutexClassTopic.asp

Your sample is ok, but I've to lose problem, when two or more users (or one user in two or more sessions) want to start my aplication. I'll have to block ability to create more then one instance of my application in one session, so "Test" is too global.

Thanx anyway
Adam


PlausiblyDamp said:
Checking for the process by name isn't an atomic operation and as such if two instances where launched very close together it is possible for the second to not detect the first.

Joe Mamma's suggestion of using a mutex is more reliable, quick sample
Visual Basic:
    Dim m As Threading.Mutex

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b As Boolean
        m = New Threading.Mutex(True, "Test", b)

        If b = True Then
            'already running
        Else
            'guess what - not running
        End If


    End Sub
 
Last edited:
Are you saying you want to limit the instances of some class to one per subnet? that is if one user starts the application and the class is instanced, no other user in the subnet can start the application?

Is this for licensing purposes?

If the above is true, you need to look at ServicedComponent and the ObjectPoolingAttribute, couple this with .NET remoting.

This can get pretty complex. So if its not for licensing, I think an architecture redesign may be in order.

If it is for licensing, I will sell you a universal solution for US$995.

It consists of server side stub that instances a pooled object. if it finds the pooled object available in the subnet it won't instance. If it doesn't find the pooled object it loads the pooled object into the machines Enterprise services and prevents other objects from being loaded in the subnet. You control the number of instances overriding the GetLicense method of the stub to read the number of licenses from some source like an encrypted license file.

Then there is a client side to the class that reads from the config where to find the server stub.

so I don't sound like a money grubber, the solution is based around the attached code. but the code here doesnt manage the remoting of your object. . . basically in DynTest is Class1 and the dynamic pooled object is defined in the main procedure. to use it in a remoting situation, you would define a class with constructor to look for other instances of the pooled object in the network, if none are found it would dynamically define and load the pooled object.
 

Attachments

Changes things a bit, a Mutex can have local (session only) scope or global (entire server) when running under terminal services clicky for details

So try changing the above code to
Visual Basic:
Dim m As Threading.Mutex

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim b As Boolean
        m = New Threading.Mutex(True, "Local\\AppNameHere" , b)

        If b = True Then
            'already running
        Else
            'guess what - not running
        End If


    End Sub
and it should work, not got a TS handy and the local citrix server is being rebuilt so I haven't had chance to test the above code.
 
Last edited:
No, it's not for licensing purposes :). I'm bulding some kind of standalone host for net. remoting.

But thanx for the offer and description.


Joe Mamma said:
Is this for licensing purposes?

If the above is true, you need to look at ServicedComponent and the ObjectPoolingAttribute, couple this with .NET remoting.

This can get pretty complex. So if its not for licensing, I think an architecture redesign may be in order.

If it is for licensing, I will sell you a universal solution for US$995.
 
Back
Top