Solved

Disable double execution of a program

Posted on 1998-12-29
8
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

688 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