Solved

Single Instance of Console App?

Posted on 2000-04-13
5
363 Views
Last Modified: 2013-11-20
Can anyone suggest me a fool proof way to limit a console app to single instance?

If somebody tries to start the console app in any or all the possible ways it should pop up th eprevious instance.

0
Comment
Question by:nv3prasad
[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
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:MT_MU
ID: 2713809
Some good info can be found at...

http://www3.pgh.net/~newcomer/nomultiples.htm
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2717929

You can just create a named, global object and check for it before continuing.

"popping up" another instance isn't allowable: what if the user is typing in some other window? Abruptly popping up another window without the user expecting it is rude. (And, Windows 2000 and newer are _finally_ actively preventing apps from performing this miserable behaviour!)

-- begin file only1.cpp --

// compile with
//   cl /W4 only1.cpp

#include <stdio.h>
#include <windows.h>

void main()
{
   HANDLE hPrev = CreateEvent(NULL, FALSE, FALSE, "SomeUniqueNameHere");
   if (GetLastError() == ERROR_ALREADY_EXISTS)
   {
      printf("Already running!\n");
      return;
   }

   printf("Not already running.\n");
   printf("Press a key to exit\n");
   getchar();
}

-- end of only1.cpp --

..B ekiM
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2728107
I tried what you said but does not seem to work, can you post me a sample which can have only one instance of a console application.

My E-mail is pnarahar@hotmail.com
0
 
LVL 1

Author Comment

by:nv3prasad
ID: 2728680
Hi Mike,

I figured out that you were not wrong and it was my mistake to reject your answer.

I will award you the points.

Can you help me to close the console window without it asking me "Press any key to continue" ( this is not your code though!)

Thank you

nv3prasad
0
 
LVL 11

Accepted Solution

by:
mikeblas earned 50 total points
ID: 2729035
> Can you help me to close the console window without
 > it asking me "Press any key to continue" ( this is not
 > your code though!)

If it's not my code, then where is it coming from? Which OS are you using?

..B ekiM
0

Featured Post

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 89
zeroFront challenge 7 122
sumHeights  challenge 17 79
Problem to event 3 112
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

756 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