hrabia Posted November 3, 2004 Posted November 3, 2004 How to check that only one instance (pro user) of my application is running? Quote A man and a dog have an average of three legs. Beaware of Statistics.
Joe Mamma Posted November 3, 2004 Posted November 3, 2004 How to check that only one instance (pro user) of my application is running?look into 'Mutex' Quote 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.
Rick_Fla Posted November 3, 2004 Posted November 3, 2004 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 Quote "Nobody knows what I do until I stop doing it."
Administrators PlausiblyDamp Posted November 3, 2004 Administrators Posted November 3, 2004 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 Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hrabia Posted November 4, 2004 Author Posted November 4, 2004 (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 November 4, 2004 by hrabia Quote A man and a dog have an average of three legs. Beaware of Statistics.
Administrators PlausiblyDamp Posted November 4, 2004 Administrators Posted November 4, 2004 If they are logging on to the same physical PC (i.e XP with user switching) then create the Mutex name based on a string concatenated with their logon ID. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hrabia Posted November 4, 2004 Author Posted November 4, 2004 What about terminal client and two or more admin users on the same server? Quote A man and a dog have an average of three legs. Beaware of Statistics.
Joe Mamma Posted November 4, 2004 Posted November 4, 2004 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 Quote 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 PlausiblyDamp Posted November 4, 2004 Administrators Posted November 4, 2004 (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 November 4, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
hrabia Posted November 5, 2004 Author Posted November 5, 2004 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. Quote A man and a dog have an average of three legs. Beaware of Statistics.
hrabia Posted November 5, 2004 Author Posted November 5, 2004 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. Quote A man and a dog have an average of three legs. Beaware of Statistics.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.