一、利用进程间同步实现
效果:
代码:
static
void
Main()
{
bool b;
Mutex m = new Mutex( true, "JueJue1984", out b );
if( !b )
{
MessageBox.Show( "³ÌÐòÒѾÔÚÔËÐÐÁË£¡", "Ô¹ⱦºÐ2007V1.0" );
}
else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run( new MyCode() );
m.ReleaseMutex();
}
}
二、用句柄实现
效果:
代码:
public
static
void
Main()
{
try
{
IntPtr hMutex=ApiFunction.CreateMutex(null,false,"jbgh");
if (ApiFunction.GetLastError()!=ApiFunction.ERROR_ALREADY_EXISTS)
{
Application.ThreadException +=new System.Threading.ThreadExceptionEventHandler(OnThreadException);
Application.Run(new FrmMdiMain());
}
else
Application.Exit();
}
else
{
MessageBox.Show("Ò½ÔºÐÅϢϵͳÒѾÔËÐУ¡","Ìáʾ",MessageBoxButtons.OK,MessageBoxIcon.Information);
ApiFunction.ReleaseMutex(hMutex);
}
}
catch
(Exception err)
{
MessageBox.Show(err.Message,"´íÎó",MessageBoxButtons.OK,MessageBoxIcon.Warning);
Application.Exit();
}
}
private
static
void
OnThreadException(
object
sender, ThreadExceptionEventArgs t)
{
MessageBox.Show(t.Exception.Message,"´íÎó",MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
效果:
代码:
















二、用句柄实现
效果:
代码:































