• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Multiple Instances of an Application

I know how to use FindWindow to look for an instance of
an application, but I need to keep track of how many
instances of the application are running.  I tried using an
INI file to do this but it is unreliable.  If the program terminates
abnormally the counters do not decrement and the INI will show
that the an instance is still running.
How can I tell how many instances are running.
I am Using Windows 95 and VC++5.0
0
jrmcg
Asked:
jrmcg
  • 9
  • 9
  • 7
1 Solution
 
nietodCommented:
If this is only for windows 95 (not NT)  you can use CreateToolhelp32Snapshot() to create a snapshot of all the processes currently ruinning.  Then use Process32First() and Processs32Next() to obtai ifo about the processes.
0
 
alexoCommented:
Gaaaaak!!!

You can set up a structured exception handler (SEH) in your program to be called in case of an "abnormal termination" and make the cleanup.

I would also use shared memory and interlocked increment/decrement a counter instead of using INI files.  At least the user will not be able to screw your app by deleting or manually changing the file.
0
 
nietodCommented:
Who was the Gaaaaak to?  (I like my solution, it doesn't seem deserving of a Gaaaaak, well maybe a gaak or a gaaak.)
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!

 
alexoCommented:
What happens when a user renames the executable?

0
 
nietodCommented:
true.  Although you can get the path and name of the current executable.  When I first answered I was thinking in terms of getting the count of another program that is running, not one that you've written.  I'm not sure why, since the question makes clear this is for a program he is writting.

I guess another approach would be to use a named semaphore...and the SEH.  That way you wouldn't need the shared memory.  
0
 
alexoCommented:
I was about to suggest a semaphore myself as a way of LIMITING the the number of instances.  However it seems jrmcg wants to QUERY the number of instances.  How do you do it with a semaphore?

jrmcg, any comments?
0
 
nietodCommented:
I think its just the two of us here having a chat...

You can use ReleaseSemaphore to find out the semaphore's count.  (I believe--never tried)  If you indicate too high a value to add on to the current value (so it would go past the maximum), ReleaseSemaphore() won't change the semaphore.  It will however, or should, still return the count.
0
 
jrmcgAuthor Commented:
The semaphore sounds good.  The ini file gives a maximum number of instances that will be allowed to run.  So if the semaphore can limit the number by initially reading an ini file for the maximum allowed it seems that this would be the best possible way to do it.  Can you give me a little more info on using the semaphore?  I've haven't used them before.

Thank you for your help -- jrmcg

0
 
alexoCommented:
Easy.

Given a global definition:

    HANDLE hSem;

In the beginning of your WinMain() function you write:

    hSem = CreateSemaphore(NULL, count, count, "CounterForMyApp");
    if (WaitForSingleObject(hSem, 0) == WAIT_TIMEOUT)
    {
        // Display an error or something...
        return 0;
    }

When you're about to exit the application write:

    ReleaseSemaphore(hSem, 1, NULL);

In order to the semaphore lock count to decrease even whan the app crashes, you have to  set up a SEH (see my previous comments) and ReleaseSemaphore() from there.

Check the __try, __except and __finally statements of your compiler.

[Todd, I claim this one.  Defend it of yield.  En guarde!]
0
 
nietodCommented:
Look at the docs on CreateSemaphore() and WaitForSingleObject() first.

Then ask questions.  

But just to give you a roadmap.  You will be using a named semaphore.  That is when you create a semaphore you will give it a name.  (could come fromt he .ini file).  When the program starts up it will create a semaphore with a specified name.  The first program that creates the named sempahore with CreateSemphore() will actually create a new semaphore.  All subsequent attempts will actually just get a handle back to the existing semaphore.
The semaphore's initial count will be the number of instances allowed to run.

The programs will do a WaitForSingleObject() on the semaphore.  if the semaphore's count is positive, indicating less than the maximum instances are running, the procedure will decrement the semaphore's count and return and the program can continue.  If the maximum has been reached, the semaphore's count will be zero.  In this case one of two things will happen.  You can make the wait procedure wait indefinitly.  Thus it will wait until another instance ends and then it will return so that the new program can continue.  More likely what you want it to have the
wait time out quickly.  Then you will look at the return value of the wait procedure to see if the time ran out (indicating the maximum was reached, and if so you should abort the program)  or if you succeeded in gettng a count from the semaphore.

Make sense?  You will need to call CloseHandle() when you are done with the semaphore.  
Alex, Do you know if you need to call ReleaseSemaphore() or will CloseHandle() do that?
0
 
nietodCommented:
touchee.  I'm a bit slow today.  I didn't realize I had been challenged until I had bled to death.
0
 
jrmcgAuthor Commented:
Excellent.  Sounds like it will do the trick.
I appreciate the help.  That was more than
enough to get me started.  I hope you both
have a great weekend.

jrmcg
0
 
alexoCommented:
>>  Do you know if you need to call ReleaseSemaphore() or will CloseHandle() do that?
AFAIK, ReleaseSemaphore() should be explicitly called.  One of the reason is that semaphores (as opposed to mutexes) are not "owned" by the thread that grabbed them.

>> touchee [...]
Yea, right!  The chance of beating a locked question is very slim nowadays.  New users do not seem to understand the concept of locked questions.  The mechanism needs rethinking.
0
 
jrmcgAuthor Commented:
How would you beat a locked question?  I do still have a question out concerning buffer space and sockets that no one seems to be able to help me with.
0
 
alexoCommented:
>> How would you beat a locked question?

jrmcg, you were hit by an exchange between Todd and me.  Let me explain...

After you submit a question to EE an expert may either answer it or leave a comment.  Once a question is answered, it is considered "locked" by the answerer.  That means that there can be only one pending answer to a question.

Now, the original customer does not always realize that he has several options:

1. He can accept the answer by grading it.  The answerer gets the points.  The question is then considered "answered".  Discussion can continue but other people have to pay points (10% of the original value) to juin the discussion, so usually nobody does.

2. He can reject ("reopen") the question.  Other experts may then lock it with (hopefully) better answers.

3. He can defer the choice until later and leave the question locked until he gets more clarifications from the answerer in the form of comments.

What hapened here is that Todd answered the question.  Later I submitted my answer (in the form of comment, since the answer was locked) which I thought was a bit more suitable for your needs.  You obviously felt differently since you accepted Todd's answer.

"beating a locked question" means that the customer has to reject the pending answer, post a comment saying that he likes the comment submitted by another expert, wait for that other expert to lock the question and finally grade him.
This procedure requires some understanding of the EE model which some inexperienced users lack.  Therefore more often then not the quickest answer gets the points.

I'll look at your other question (no promises...)
0
 
jrmcgAuthor Commented:
I was under the impression that if you reject an answer it looks bad on the person asking the question and may keep other experts from answering questions in the future.  In other words I thought that was like giving an F grade.

-- If you can help me with the sockets problem I would more than gladly increase the points.

Thanks -- JR

0
 
nietodCommented:
There are no F grades.  When you reject an answer the expert does not get a grade (F or otherwise).  It is ussually a good idea to try to work with an expert that has answered your question.  Like if you don't completely understand them, ask them for a clarification or additional information rather than rejecting the answer.  Rejecting an answer under those conditions may make an expert angry.  But, if in the end the expert who answered didn't help you and another expert did, then you should reject the useless answer and request the expert who really helped to answer.  

Its up to you to decide who provided the most help and then to see to it that they get the grade.
0
 
jrmcgAuthor Commented:
Thank you for the info.  I will definitely use it in the future.

Thanks - JR

0
 
alexoCommented:
>> If you can help me with the sockets problem I would more than gladly increase the points.

Sorry.  Outside my area of knowledge.
0
 
nietodCommented:
If the question is old, you might want to try deleting it and asking it again.  That puts it back at the top of the list.  Sometimes that helps.  I saw one answered within about an hour that way.
0
 
alexoCommented:
You cannot delete a question if it has pending, accepted or rejected answers.
0
 
nietodCommented:
I didn't look at the question to see.  I assumed there were no answers.  When you assume you make....
0
 
jrmcgAuthor Commented:
After the question sits out there long enough EE will delete it and give me my points back. I rejected the answer that was given after I added a comment asking for clarification and didn't get a reply after 3 or 4 days.  That is the second time I've asked that question.  I thought the first answer had fixed it but it started happening again.
0
 
alexoCommented:
>> After the question sits out there long enough EE will delete it [...]
21 days of no activity.  Somebody adds a comments and it's another 21 days...
Better ask EE customer support to remove it.
0
 
jrmcgAuthor Commented:
Thanks I'll do that
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!

  • 9
  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now