保证应用程序只有一个实例在运行
来源:互联网 发布:mac如何修改文件后缀 编辑:程序博客网 时间:2024/05/21 17:18
[C#] |
public static Process RunningInstance() |
{ |
Process current = Process.GetCurrentProcess(); |
Process[] processes = Process.GetProcessesByName (current.ProcessName); |
//Loop through the running processes in with the same name |
foreach (Process process in processes) |
{ |
//Ignore the current process |
if (process.Id != current.Id) |
{ |
//Make sure that the process is running from the exe file. |
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") == current.MainModule.FileName) |
{ |
//Return the other process instance. |
return process; |
} |
} |
} |
//No other instance was found, return null. |
return null; |
} |
[VB.NET] |
Public Shared Function RunningInstance() As Process |
Dim current As Process = Process.GetCurrentProcess() |
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName) |
'Loop through the running processes in with the same name |
Dim process As Process |
For Each process In processes |
'Ignore the current process |
If process.Id <> current.Id Then |
'Make sure that the process is running from the exe file. |
If [Assembly].GetExecutingAssembly().Location.Replace("/", "/") = current.MainModule.FileName Then |
'Return the other process instance. |
Return process |
End If |
End If |
Next process |
'No other instance was found, return null. |
Return Nothing |
End Function 'RunningInstance |
如果检测到已有窗体实例,将此实例显示出来参考:
[STAThread] static void Main() { //Get the running instance. Process instance = RunningInstance(); if (instance == null) { //There isn't another instance, show our form. Application.Run (new Form2()); } else { //There is another instance of this process. HandleRunningInstance(instance); } } public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name foreach (Process process in processes) { //Ignore the current process if (process.Id != current.Id) { //Make sure that the process is running from the exe file. if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") ==current.MainModule.FileName) { //Return the other process instance. return process; } } } //No other instance was found, return null. return null; } public static void HandleRunningInstance(Process instance) { //Make sure the window is not minimized or maximized ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to foreground window SetForegroundWindow (instance.MainWindowHandle); } [DllImport("User32.dll")] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); private const int WS_SHOWNORMAL = 1 |
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行
- 保证C#应用程序只有一个实例在运行
- VC保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例在运行——VS2008
- VC保证应用程序只有一个实例在运行
- 保证应用程序只有一个实例运行.txt
- 保证只有一个实例运行
- 保证一个程序只有一个应用程序在运行
- 保证某一执行文件只有一个实例在运行
- 保证程序只有一个运行实例
- 保证当前只有一个程序在运行
- 如何使应用程序只有一个实例在运行
- 如何确保只有一个应用程序实例在运行?
- win98下配置jsp的运行环境实践
- 彻底解决JSP(含EJB)中文内码问题
- 缩小数据库日志另类方法
- 更改数据库所有者的对象
- 我的程序人生
- 保证应用程序只有一个实例在运行
- 使用WMI获得硬盘的信息
- 一个关于IIS的类
- 关于硬盘序列号的类
- Struts 学习笔记1 -Struts Framework 概览
- 深入探讨MFC消息循环和消息泵
- 【CSplitterWnd技巧一】改变分割窗口拖动条的大小
- 【CSplitterWnd技巧二】使分割窗口不能改变大小
- 【IPC-钩子】WM_COPYDATA和鼠标钩子小程序