"ygy" <yy@spm.com.cn> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...
I want my programn run for once.Forbid the programn run when the
programn has run.I know I can use CWnd::FindWindow() funtion in
C++(MFC).What should I do in C#.Thank you!
It was nice and easy in VB with App.PrevInstance, wasn't it...? :-)
I use a Mutex for this in C#...
System.Threading.Mutex objMutex = null;
this.Visible = false;
// check there isn't already an instance of this app
running ------------------------------------------
try
{
bool blnCreatedMutex;
objMutex = new System.Threading.Mutex(true, "<MyAppName>", out
blnCreatedMutex);
if (!blnCreatedMutex)
{
MessageBox.Show("<MyAppName> is already running", "<MyAppName>",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
this.Close();
return;
}
else
{
GC.KeepAlive(objMutex);
this.Visible = true;
}
}
catch (Exception ex)
{
// do your error handling here
return;
}