I hope this is right forum for this...

I have the below block of code in my Main. When I compile in debug mode, I
get to run one instance only. When I compile in release mode, I get to run
more than one instance of my application. These are the first lines of my
application.. I tried to set the mutexName to a string or a GUID or the full
assembly name, still I can't get single Instance behaviour in Release mode

bool mutexWasCreated = false;
bool requestInitialOwnership = true;
string mutexName = "MyApp";
//System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Reflection.Assembly.GetExecutingAssembly()).ToString();
//System.Reflection.Assembly.GetExecutingAssembly().FullName;

System.Threading.Mutex m = new
Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);

// If I put a log information here. I get True also..

try
{
if (mutexWasCreated)
{
m.ReleaseMutex();
}
else
{
MessageBox.Show("Application is already
running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
finally
{
m.Close();
}

// Code here calls Application.Run

RE: Mutex... by JCauble

JCauble
Tue Jan 10 09:00:03 CST 2006

This works for me. include the system.diagnostics namespace and here is my
code for the Main function:

static void Main()
{
string appName =
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;

Process[] pi = Process.GetProcessesByName(appName);

if (pi.GetUpperBound(0) > 0)
{
MessageBox.Show("This application is already running.");

Application.ExitThread();
Application.Exit();
}
else
{
Application.Run(new Form1());
}
}

Hope that helps.

Thanks.

"VJ" wrote:

> I hope this is right forum for this...
>
> I have the below block of code in my Main. When I compile in debug mode, I
> get to run one instance only. When I compile in release mode, I get to run
> more than one instance of my application. These are the first lines of my
> application.. I tried to set the mutexName to a string or a GUID or the full
> assembly name, still I can't get single Instance behaviour in Release mode
>
> bool mutexWasCreated = false;
> bool requestInitialOwnership = true;
> string mutexName = "MyApp";
> //System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Reflection.Assembly.GetExecutingAssembly()).ToString();
> //System.Reflection.Assembly.GetExecutingAssembly().FullName;
>
> System.Threading.Mutex m = new
> Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);
>
> // If I put a log information here. I get True also..
>
> try
> {
> if (mutexWasCreated)
> {
> m.ReleaseMutex();
> }
> else
> {
> MessageBox.Show("Application is already
> running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
> return;
> }
> }
> finally
> {
> m.Close();
> }
>
> // Code here calls Application.Run
>
>
>
>
>

Re: Mutex... by Brian

Brian
Tue Jan 10 09:27:31 CST 2006

"VJ" <vijaybalki@yahoo.com> wrote in message
news:ePTBmYXFGHA.3728@tk2msftngp13.phx.gbl...
> I hope this is right forum for this...
>
> I have the below block of code in my Main. When I compile in debug mode, I
> get to run one instance only. When I compile in release mode, I get to run
> more than one instance of my application. These are the first lines of my
> application.. I tried to set the mutexName to a string or a GUID or the
full
> assembly name, still I can't get single Instance behaviour in Release mode
>
> bool mutexWasCreated = false;
> bool requestInitialOwnership = true;
> string mutexName = "MyApp";
>
//System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Re
flection.Assembly.GetExecutingAssembly()).ToString();
>
//System.Reflection.Assembly.GetExecutingAssembly().FullName;
>
> System.Threading.Mutex m = new
> Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);
>
> // If I put a log information here. I get True also..
>
> try
> {
> if (mutexWasCreated)
> {
> m.ReleaseMutex();
> }
> else
> {
> MessageBox.Show("Application is already
> running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
> return;
> }
> }
> finally
> {
> m.Close();
> }
>
> // Code here calls Application.Run
>


I've been succuessful this way

bool owner = false;
Mutex mutex = new Mutex(true, "Global\\" +
Assembly.GetExecutingAssembly().FullName, out owner);

if (owner)
{
AppFoo foo = new AppFoo();
try
{
//Start Application
Application.Run(foo.MainForm);
}
finally
{
mutex.ReleaseMutex();
}
}
else
{
//Show existing Application
IntPtr hWnd = NativeMethods.FindWindow(Application.ProductName);
if (hWnd != IntPtr.Zero)
{
NativeMethods.ShowWindow(hWnd, NativeMethods.ShowCmd.Restore );
NativeMethods.SetForegroundWindow(hWnd);
}
}



Re: Mutex... by VJ

VJ
Tue Jan 10 09:51:34 CST 2006

Thanks Brian. That did it. What was wrong in the way I was doing it??

Vijay

"Brian Richards" <brichards@gmail.com> wrote in message
news:%23r9jjpfFGHA.140@TK2MSFTNGP12.phx.gbl...
> "VJ" <vijaybalki@yahoo.com> wrote in message
> news:ePTBmYXFGHA.3728@tk2msftngp13.phx.gbl...
>> I hope this is right forum for this...
>>
>> I have the below block of code in my Main. When I compile in debug mode,
>> I
>> get to run one instance only. When I compile in release mode, I get to
>> run
>> more than one instance of my application. These are the first lines of my
>> application.. I tried to set the mutexName to a string or a GUID or the
> full
>> assembly name, still I can't get single Instance behaviour in Release
>> mode
>>
>> bool mutexWasCreated = false;
>> bool requestInitialOwnership = true;
>> string mutexName = "MyApp";
>>
> //System.Runtime.InteropServices.Marshal.GetTypeLibGuidForAssembly(System.Re
> flection.Assembly.GetExecutingAssembly()).ToString();
>>
> //System.Reflection.Assembly.GetExecutingAssembly().FullName;
>>
>> System.Threading.Mutex m = new
>> Mutex(requestInitialOwnership,mutexName,out mutexWasCreated);
>>
>> // If I put a log information here. I get True also..
>>
>> try
>> {
>> if (mutexWasCreated)
>> {
>> m.ReleaseMutex();
>> }
>> else
>> {
>> MessageBox.Show("Application is already
>> running","MyApp",MessageBoxButtons.OK,MessageBoxIcon.Error);
>> return;
>> }
>> }
>> finally
>> {
>> m.Close();
>> }
>>
>> // Code here calls Application.Run
>>
>
>
> I've been succuessful this way
>
> bool owner = false;
> Mutex mutex = new Mutex(true, "Global\\" +
> Assembly.GetExecutingAssembly().FullName, out owner);
>
> if (owner)
> {
> AppFoo foo = new AppFoo();
> try
> {
> //Start Application
> Application.Run(foo.MainForm);
> }
> finally
> {
> mutex.ReleaseMutex();
> }
> }
> else
> {
> //Show existing Application
> IntPtr hWnd = NativeMethods.FindWindow(Application.ProductName);
> if (hWnd != IntPtr.Zero)
> {
> NativeMethods.ShowWindow(hWnd, NativeMethods.ShowCmd.Restore );
> NativeMethods.SetForegroundWindow(hWnd);
> }
> }
>
>



Re: Mutex... by Jon

Jon
Tue Jan 10 17:45:30 CST 2006

VJ <vijaybalki@yahoo.com> wrote:
> Thanks Brian. That did it. What was wrong in the way I was doing it??

<snip>

See http://www.pobox.com/~skeet/csharp/faq/#one.application.instance

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too