?
Solved

Disable double execution of a program

Posted on 1998-12-29
8
Medium Priority
?
188 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
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!

 
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 200 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

801 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