Solved

Disable double execution of a program

Posted on 1998-12-29
8
180 Views
Last Modified: 2010-04-02
I am writing an MFC dialog based program, and wouldn't like the user to open multiple copies of the program at once.
(only one at a time).
How is this done?
Thanks
0
Comment
Question by:gilbert_chang
  • 3
  • 3
  • 2
8 Comments
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181210
Do you want only the user to use that program ONLY, or what?
If it is the only application that needs attention then you could force the cursor to remain within the dialog, else you could use a detect which I am not fimiliar with.  (All though I would prob. say don't open two apps.

I would write a file which holds a value 1 or 0, and that will tell me if the application is allready running.

when you are in INITDialog read the value and use

if (!value)
   return;

this will quit the second app before it even comes up.  hope this helps.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181211
Do you want only the user to use that program ONLY, or what?
If it is the only application that needs attention then you could force the cursor to remain within the dialog, else you could use a detect which I am not fimiliar with.  (All though I would prob. say don't open two apps.

I would write a file which holds a value 1 or 0, and that will tell me if the application is allready running.

when you are in INITDialog read the value and use

if (!value)
   return;


You will say on Create, make value=1 and writeover the file.
and at the end rewrite to 0.
this will quit the second app before it even comes up.  hope this helps.
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1181212
if you want me to eleb, then I will, but just create a file and add char Value=1;  when the app starts.  (OnCreate)
then when it closes (fp.write("0");  (something to that effect);


0
 
LVL 11

Expert Comment

by:alexo
ID: 1181213
Here is the "official" way of limiting a program to only one instance:

    // Beginning of program
    HANDLE hMutex;
    hMutex = CreateMutex(NULL, FALSE, "Some unique string");
    if (GetLastError() == ERROR_ALREADY_EXISTS)
    {
        // Application already running, display an error or something
        CloseHandle(hMutex);
        return 0;
    }

    // The rest of the program goes here...

    // End of program
    CloseHandle(hMutex);
    return 1;

If this is what you need, you'll need to reject the currently pending answer so you can accept mine.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:gilbert_chang
ID: 1181214
alexo's answer works great. Thanks The_Brain too.
0
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1181215
Details in my comment from December 29 1998 - 05:29AM
0
 

Author Comment

by:gilbert_chang
ID: 1181216
Thanks.
0
 

Author Comment

by:gilbert_chang
ID: 1181217
Thanks.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now