Results 1 to 16 of 16

Thread: Limit the amount of times your application could be opened.

  1. #1

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Arrow 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

    VB.net Code:
    1. Imports System.Threading
    2. Public Class Form1
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim Mutex_Object As Mutex 'We will be working with Mutex
    5.         Mutex_Object = New Mutex(False, "Pervent Twice Open") ' Were you see "Pervent Twice Open" thats were you put the name of your project.
    6.         If Mutex_Object.WaitOne(0, False) = False Then 'lets perform the check
    7.             MessageBox.Show("The same application is opened twice.")
    8.             Application.Exit()
    9.         End If
    10.     End Sub
    11. End Class

    P.s
    I have no idea were i found this code.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  2. #2
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Limit the amount of times your application could be opened.

    Is this the same as making a single instance application?
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  3. #3

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    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:
    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.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    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...
    Last edited by Arve K.; Sep 8th, 2010 at 02:46 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  6. #6

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    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.
    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.
    Anyways rate me if you found this intersecting
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  7. #7
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: Limit the amount of times your application could be opened.

    Quote Originally Posted by Pc_Not_Mac View Post
    Anyways rate me if you found this intersecting
    Already have
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  8. #8

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Re: Limit the amount of times your application could be opened.

    Thanks powerade
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  9. #9
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    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.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  10. #10

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    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.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  11. #11
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: Limit the amount of times your application could be opened.

    Couldnt you use the Mutex, way to prevent the opening of other programs?

  12. #12

    Thread Starter
    Frenzied Member Pc_Not_Mac's Avatar
    Join Date
    Oct 2009
    Location
    localhost
    Posts
    1,206

    Re: Limit the amount of times your application could be opened.

    Try it.
    My Codebank:
    Windows Vista & 7 Glass Effect & Limit the amount of times your application could be opened.
    Pause Your Code & Check the OS name

    The question of whether computers can think is like the question of whether submarines can swim.

    Currently learning: Java

    Coding can be a learning experience or

  13. #13
    Banned
    Join Date
    Mar 2009
    Posts
    764

    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 ?

  14. #14
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  15. #15
    Banned
    Join Date
    Mar 2009
    Posts
    764

    Re: Limit the amount of times your application could be opened.

    do you think such code exists(for trial apps) ?

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width