Thank you Jon and Mark for finding out the reason. I can reproduce the
problem now with this piece of code: (add GC.Collect())
[STAThread]
static void Main()
{
bool bOwnInitially = true;
bool bCreated;
Mutex m = new Mutex(bOwnInitially,
"2ba3dbae-eb22-4712-81e1-638a10a33ab3", out bCreated);
if (!(bCreated && bOwnInitially))
{
MessageBox.Show(Resources.InstanceAlreadyRunning, "My App",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
GC.Collect();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
As for the solution, both GC.KeepAlive and GCHandle.Alloc should help if
they are used properly. For example:
=== GC.KeepAlive ===
[STAThread]
static void Main()
{
bool bOwnInitially = true;
bool bCreated;
Mutex m = new Mutex(bOwnInitially,
"2ba3dbae-eb22-4712-81e1-638a10a33ab3", out bCreated);
if (!(bCreated && bOwnInitially))
{
MessageBox.Show(Resources.InstanceAlreadyRunning, "My App",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
GC.KeepAlive(m); // keep the obj m alive in main.
}
=== GCHandle.Alloc ===
[STAThread]
static void Main()
{
bool bOwnInitially = true;
bool bCreated;
Mutex m = new Mutex(bOwnInitially,
"2ba3dbae-eb22-4712-81e1-638a10a33ab3", out bCreated);
GCHandle handle = GCHandle.Alloc(m); // allocate gc handle
if (!(bCreated && bOwnInitially))
{
MessageBox.Show(Resources.InstanceAlreadyRunning, "My App",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
handle.Free(); // release gc handle.
}
Regards,
Jialiang Ge (
[email protected], remove 'online.')
Microsoft Online Community Support
=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================