Solved

Single Instance of Console App?

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

Suggested Solutions

Title # Comments Views Activity
VB.NET how to use the Vertical ScrollBar 5 113
Shell script to login to remote m/c and execute commands 10 78
haveThree challenge 22 121
Work with App store 7 67
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

789 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