如何只让Windows程序只启动一个实例,第二个实例启动只激活第一个实例

ExpandedBlockStart.gif ContractedBlock.gif /**/ /// <summary>
InBlock.gif
/// 从这里开始运行
ExpandedBlockEnd.gif
/// </summary>

None.gif [STAThread]
None.gif
static   void  Main() 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif Process instance 
= RunningInstance();
InBlock.gif 
if (instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
//没有实例在运行
InBlock.gif
  WeatherApp appInstance = new WeatherApp();
InBlock.gif  appInstance.StartMainGui();
ExpandedSubBlockEnd.gif }

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
//已经有一个实例在运行
InBlock.gif
  HandleRunningInstance(instance);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

ContractedBlock.gifExpandedBlockStart.gif
确保只有一个实例 #region 确保只有一个实例
InBlock.gif
public static Process RunningInstance()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif Process current 
= Process.GetCurrentProcess();
InBlock.gif Process[] processes 
= Process.GetProcessesByName (current.ProcessName);
InBlock.gif 
//遍历与当前进程名称相同的进程列表
InBlock.gif
 foreach (Process process in processes)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
//Ignore the current process
InBlock.gif
  if (process.Id != current.Id)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
//Make sure that the process is running from the exe file.
InBlock.gif
   if (Assembly.GetExecutingAssembly().Location.Replace("/""\\"== current.MainModule.FileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    
//Return the other process instance.
InBlock.gif
    return process;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
return null;
ExpandedSubBlockEnd.gif}

InBlock.gif
private static void HandleRunningInstance(Process instance)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif MessageBox.Show(
"该应用系统已经在运行!","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Information);
InBlock.gif ShowWindowAsync(instance.MainWindowHandle,
1); //调用api函数,正常显示窗口
InBlock.gif
 SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
ExpandedSubBlockEnd.gif
}

InBlock.gif[DllImport(
"User32.dll")]
InBlock.gif
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
InBlock.gif[DllImport(
"User32.dll")]
InBlock.gif
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
ExpandedBlockEnd.gif
#endregion

None.gif

None.gif [STAThread]
None.gif
static   void  Main( string [] args)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
bool isFirst;
InBlock.gif 
InBlock.gif System.Threading.Mutex mutex
=new System.Threading.Mutex(true,"WindowAppTest",out isFirst); 
InBlock.gif 
//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突     
InBlock.gif
 if(!isFirst)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  MessageBox.Show(
"Exist");
InBlock.gif  Environment.Exit(
1);//实例已经存在,退出程序
ExpandedSubBlockEnd.gif
 }

InBlock.gif 
else
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  Application.Run(
new Form1());
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/iyond/archive/2005/10/22/259800.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值