.net2005,用互斥来控制,程序只启动一次的一点经验(万恶的"release")
2006-07-27 11:00
465 查看
首先我的代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool isExist;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mutex MyMutex = new Mutex(true,"MySoft",out isExist);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (isExist)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//程序运行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Run(new MainForm());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//show ErrorMessage Application terminate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
好好的代码在"debug"模式下正常运行,呵呵,运行的挺美~
要发布了,把配置改为"release",把生成中的调试信息改为"none"(.net2003的发布根本不用改)
结果:哇!~互斥不起做用了,把"release"模式下的调试信息改为"full",互斥有起作用了,不知道为什么,
但是觉得平白多个pdb文件,很不舒服~
最后从网上找了点资料整理出来,贡献出来:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Diagnostics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Reflection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private const int WS_SHOWNORMAL = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private static extern bool SetForegroundWindow(IntPtr hWnd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static Process RunningInstance()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process current = Process.GetCurrentProcess();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process[] processes = Process.GetProcessesByName(current.ProcessName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Loop through the running processes in with the same name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (Process process in processes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Ignore the current process
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (process.Id != current.Id && Assembly.GetExecutingAssembly().Location.Replace("/", "/") == current.MainModule.FileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return process;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static bool HandleRunningInstance(Process instance)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
instance.WaitForInputIdle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//确保窗口没有被最小化或最大化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置真实例程为foreground window
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return SetForegroundWindow(instance.MainWindowHandle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process MYSoft = RunningInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(MYSoft ==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//启动程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//激活已启动的程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HandleRunningInstance(MYSoft);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
bool isExist;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Mutex MyMutex = new Mutex(true,"MySoft",out isExist);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
if (isExist)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//程序运行
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Application.Run(new MainForm());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//show ErrorMessage Application terminate
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
好好的代码在"debug"模式下正常运行,呵呵,运行的挺美~
要发布了,把配置改为"release",把生成中的调试信息改为"none"(.net2003的发布根本不用改)
结果:哇!~互斥不起做用了,把"release"模式下的调试信息改为"full",互斥有起作用了,不知道为什么,
但是觉得平白多个pdb文件,很不舒服~
最后从网上找了点资料整理出来,贡献出来:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Diagnostics;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Reflection;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Runtime.InteropServices;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private const int WS_SHOWNORMAL = 1;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[DllImport("User32.dll")]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private static extern bool SetForegroundWindow(IntPtr hWnd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static Process RunningInstance()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process current = Process.GetCurrentProcess();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process[] processes = Process.GetProcessesByName(current.ProcessName);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Loop through the running processes in with the same name
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (Process process in processes)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//Ignore the current process
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (process.Id != current.Id && Assembly.GetExecutingAssembly().Location.Replace("/", "/") == current.MainModule.FileName)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return process;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static bool HandleRunningInstance(Process instance)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
instance.WaitForInputIdle();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//确保窗口没有被最小化或最大化
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//设置真实例程为foreground window
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return SetForegroundWindow(instance.MainWindowHandle);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
[STAThread]
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
static void Main(string[] args)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Process MYSoft = RunningInstance();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(MYSoft ==null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//启动程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//激活已启动的程序
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
HandleRunningInstance(MYSoft);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
}
相关文章推荐
- "无法在web服务器上启动调试,不具备调试此程序的权限" 问题解决
- Android退出程序时的"再按一次退出"实现
- 用"互斥"让程序只能运行一次
- "由于应用程序配置不正确,应用程序未能启动" VS2008 Release /MD编译
- iOS开发 -- "AppDelegate.m"文件 程序启动流程
- "无法启动调试 没有正确安装调试器 请运行安装程序安装或修复调试器" VS2003错误
- "没有找到MFC80UD.DLL,因此这个程序未能启动.重新安装应用程序可能会修复此问题"?
- 如何去掉IE7启动时的"自定义您的设置"
- Java中如何克服"构造器只能使初始化工作进行一次"的"缺点"?
- "[转]以下一些使用ASP.NET和VS.NET2003的经验和技巧," 一文的评论:
- "普及正版,Trufun25000套赠送中国千家企业百所高校活动"启动
- A标签启动FTP下载 <a href=" ftp://帐号:密码@FTP网址“
- 实战:IIS错误提示:"服务没有及时响应启动或控制请求"
- 未在本地计算机上注册"Microsoft.ACE.OLEDB.12.0"提供程序
- "无法启动Web服务器。Web服务器无法找到请求的资源" 或 "unable to start debugging on the web server.the web server could not find the request"解决方法
- VS2005 解决"应用程序配置不正确,程序无法启动"问题(小问题,大思想)
- "分段好啊分段好"求最大M个数的程序 (上)2006-07-18
- "Windows无法在任务栏上显示音量控制"问题解决
- Wince下程序只启动一次控制
- 初识用UDP协议做的windows窗口聊天程序---此处是双向的"异步"多线程对话框通信.