Solved

Single Instance of Console App?

Posted on 2000-04-13
5
370 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

636 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