PC Review


Reply
Thread Tools Rate Thread

Can't find a bug in this example

 
 
dvestal@gmail.com
Guest
Posts: n/a
 
      4th Aug 2011
The code below is simplified, but it should get the point across. I
have an app that should only have one instance running at a time (it
interacts with hardware and moves files around, and doesn't work if
another instance is also doing that).

I thought I was accomplishing this by grabbing a named mutex at the
start. However, a very small number of times, a second instance of
the app manages to make it into the meat of the code (i.e., to the "Do
Stuff" comment), and I can't figure out how that's happening. Any
ideas?


using System;
using System.Collections.Generic;
using System.IO;

namespace TSSG.DeviceTesting
{
public sealed partial class TestEngine
{
[STAThread]
static public void Main(string[] commandLineArgs)
{
TestEngine te = new TestEngine();
te.RunApplication(commandLineArgs);
}

public void RunApplication(string[] commandLineArgs)
{
Mutex appExclusivityMutex = null;
bool gotMutex = false;

try
{
appExclusivityMutex = new Mutex(
false,
"Opsys Engine Exclusivity");

gotMutex = appExclusivityMutex.WaitOne(0, false);

if(!gotMutex)
{
MessageBox.Show(
"This application is already running.",
"Opsys Engine");

return;
}

// Do stuff. Problem is, sometimes we make it here
// with a different instance of the application ALSO
// running in this section of code. How is this
possible?
}
catch(Exception e)
{
}
finally
{
Cleanup();

if(gotMutex && null != appExclusivityMutex)
{
appExclusivityMutex.ReleaseMutex();
}
}
}
}
}
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find multiple characters in one find using MSword find/replace =?Utf-8?B?Q2xpZmY=?= Microsoft Word Document Management 2 29th Oct 2006 07:48 PM
Find and Advanced Find Feature cannot find any data in outlook =?Utf-8?B?VGVycmkgU2Nob2VybmVy?= Microsoft Outlook Discussion 0 30th Jan 2006 07:19 PM
How to find all the members of a group with the advance find mode in the active directory console? =?Utf-8?B?Y2F2YXRpbm8=?= Microsoft Windows 2000 CMD Promt 5 1st Feb 2004 11:27 PM
How to find all the members of a group with the advance find mode in the active directory console? =?Utf-8?B?Y2F2YXRpbm8=?= Microsoft Windows 2000 0 28th Jan 2004 02:51 AM
How to find all the members of a group with the advance find mode in the active directory console? =?Utf-8?B?Y2F2YXRpbm8=?= Microsoft Windows 2000 0 28th Jan 2004 02:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:34 PM.