Jump to content
Xtreme .Net Talk

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


Recommended Posts

Posted
How to check that only one instance (pro user) of my application is running?
look into 'Mutex'

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Try this function

 

'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

"Nobody knows what I do until I stop doing it."
  • Administrators
Posted

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

   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

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted (edited)

Thanx, I've read a little bit about mutex class:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/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

 

 

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

   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

Edited by hrabia

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted
What about terminal client and two or more admin users on the same server?

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

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.

DynamicService.zip

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

  • Administrators
Posted (edited)

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

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.

Edited by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

 

 

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.

A man and a dog have an average of three legs.

Beaware of Statistics.

Posted

Smart idea, I'll check it.

 

"Local\\AppNameHere"

 

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.

A man and a dog have an average of three legs.

Beaware of Statistics.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...