Limit the amount of times your application could be opened.
Okay so i had this code in my library for some time now.
So i'm going to share with you.
Rate if you like it :D
VB.net Code:
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Mutex_Object As Mutex 'We will be working with Mutex
Mutex_Object = New Mutex(False, "Pervent Twice Open") ' Were you see "Pervent Twice Open" thats were you put the name of your project.
If Mutex_Object.WaitOne(0, False) = False Then 'lets perform the check
MessageBox.Show("The same application is opened twice.")
Application.Exit()
End If
End Sub
End Class
P.s
I have no idea were i found this code.
Re: Limit the amount of times your application could be opened.
Is this the same as making a single instance application?
Re: Limit the amount of times your application could be opened.
I looked for "single instance application" on this form.
His example requires more code.
For example he gets all the system process first.
Loops around them to check if his application is already running and then kills it if its running twice.
My example uses Mutex.
I found a very good definition of Mutex:
Quote:
Mutex, can be thought of as a more powerful version of Monitor. Like AutoResetEvent and ManualResetEvent, it is derived from WaitHandle. An advantage of Mutex over Monitor is that you can use the methods from WaitHandle such as WaitOne. A disadvantage is that is much slower, at about half as fast as Monitor. Mutex is very useful when you must control access to a resource that could be accessed through multiple processes, like a data file used by several applications you have created. To write to the file, the writing thread must have total access to the file throughout the operating system.
Re: Limit the amount of times your application could be opened.
"Pervent" What kind of applications do YOU make?! - kidding.
Huh... that's interesting... not what I was expecting when I clicked in. (thought it was more like a trialware type of code) ... could be interesting.
-tg
Re: Limit the amount of times your application could be opened.
Oh, I meant in the project properties, there is a checkbox that says 'Make single instance application'. That will prevent the application from being opened multiple times. It's just that, at least what I can understand from the documentation, a mutex object is used to ensure that a thread is synchronized to have full access to a shared file, image or other resource that is used by other applications (or threads) at the same time. Although it seems (from your example) that you can use mutex to prevent several instances of the same application, I don't think that this is what it was designed for...
Edit:
But, as techgnome says, interesting... :)
Re: Limit the amount of times your application could be opened.
Quote:
Originally Posted by techgnome
"Pervent" What kind of applications do YOU make?! - kidding.
The program that i made unfortunately takes a lot of the CPU about 20% on a 4-core.
That's one big over grown bug if you ask me. The good news are that i could fix it.
My laptop which has only 2 cores displays 50% of CPU usage. :lol:
Make single instance application
Quote:
Originally Posted by powerade
Oh, I meant in the project properties, there is a checkbox that says 'Make single instance application'. That will prevent the application from being opened multiple times. It's just that, at least what I can understand from the documentation, a mutex object is used to ensure that a thread is synchronized to have full access to a shared file, image or other resource that is used by other applications (or threads) at the same time. Although it seems (from your example) that you can use mutex to prevent several instances of the same application, I don't think that this is what it was designed for...
I saw that checks box before.
My example just shows how you can use Mutex for such a task. :D
Anyways rate me if you found this intersecting :D
Re: Limit the amount of times your application could be opened.
Quote:
Originally Posted by
Pc_Not_Mac
Anyways rate me if you found this intersecting :D
Already have ;)
Re: Limit the amount of times your application could be opened.
Re: Limit the amount of times your application could be opened.
While this may work, the best option is just use the single instance application property in the project properties of the application.
From there, you can use the Startup event of the application to do any other work, such as messages and what not.
Re: Limit the amount of times your application could be opened.
Quote:
Originally Posted by weirddemon
While this may work, the best option is just use the single instance application property in the project properties of the application.
From there, you can use the Startup event of the application to do any other work, such as messages and what not.
ehh...
The example uses Mutex nothing more and nothing less.
Re: Limit the amount of times your application could be opened.
Couldnt you use the Mutex, way to prevent the opening of other programs?
Re: Limit the amount of times your application could be opened.
Re: Limit the amount of times your application could be opened.
how does the programmer set the amount of times the program can be opened ?
after the pc is reboot can the user reopen the program if it was already used ?
Re: Limit the amount of times your application could be opened.
The point of the code was to prevent multiple instances of the app from running at the same time... it's not the same kind of limitation as say code for a trial version of an app.
-tg
Re: Limit the amount of times your application could be opened.
do you think such code exists(for trial apps) ?
Re: Limit the amount of times your application could be opened.
uuuuh.... yeah.... I've seen some time-based ones (you have XX days left to use this application) as well as number of times (You can use this app XX more times.) The code exists... most people/places will guard that info as it could then be used to hack it... I wrote a time based one... think it's in the codebank somewhere... it's basic, but in a pinch it works. And I know I'm not the only one that's written this kind of stuff.
-tg
I'm surprised it's not in your Battle Programming book... I believe it was used in the Battle of Paradigm of '68... now that was an epic battle.