转C# 只能运行一个winForm进程
2012-10-30 10:52
162 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
namespace OnlyProcess
{
static class Program
{
[STAThread]
static void Main()
{
MessageBox.Show("ddddd");
Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行可以让窗体具有XP风格.
System.Windows.Forms.Application.DoEvents();
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
}
#region 只运行一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();//当前新启动的线程
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//process,原来旧的线程与当前新启动的线程ID不一样
//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;//返回原来旧线程的窗体
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
#endregion
}
}
//namespace OnlyProcess
//{
// static class Program
// {
// /// <summary>
// /// 应用程序的主入口点。
// /// </summary>
// [STAThread]
// static void Main()
// {
// //get the name of our process
// string proc = Process.GetCurrentProcess().ProcessName;
// //get the list of all processes by that name
// Process[] processes = Process.GetProcessesByName(proc);
// //if there is more than one process
// if (processes.Length > 1)
// {
// DialogResult dr = MessageBox.Show("Application is already running! Are you sure want to run another?", "INFORMATION", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
// if (dr == DialogResult.OK)
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
// }
// }
// else
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
// }
// }
// }
//}
C# 创建互斥进程(程序) 互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行. 现在很多软件都有这功能,如Maxthon 可以设置为"只允许打开一个窗体",还有Bitcomet等. 我也是看到这些软件的这个功能才来研究这个问题的. 要实现程序的互斥,通常有4中方式,下面用 C# 语言来实现:
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Reflection;
namespace OnlyProcess
{
static class Program
{
[STAThread]
static void Main()
{
MessageBox.Show("ddddd");
Process instance = RunningInstance();
if (instance == null)
{
System.Windows.Forms.Application.EnableVisualStyles(); //这两行可以让窗体具有XP风格.
System.Windows.Forms.Application.DoEvents();
Application.Run(new Form1());
}
else
{
HandleRunningInstance(instance);
}
}
#region 只运行一个实例
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();//当前新启动的线程
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历与当前进程名称相同的进程列表
foreach (Process process in processes)
{
//process,原来旧的线程与当前新启动的线程ID不一样
//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;//返回原来旧线程的窗体
}
}
}
return null;
}
private static void HandleRunningInstance(Process instance)
{
MessageBox.Show("该应用系统已经在运行!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //调用api函数,正常显示窗口
SetForegroundWindow(instance.MainWindowHandle); //将窗口放置最前端。
}
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(System.IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(System.IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
#endregion
}
}
//namespace OnlyProcess
//{
// static class Program
// {
// /// <summary>
// /// 应用程序的主入口点。
// /// </summary>
// [STAThread]
// static void Main()
// {
// //get the name of our process
// string proc = Process.GetCurrentProcess().ProcessName;
// //get the list of all processes by that name
// Process[] processes = Process.GetProcessesByName(proc);
// //if there is more than one process
// if (processes.Length > 1)
// {
// DialogResult dr = MessageBox.Show("Application is already running! Are you sure want to run another?", "INFORMATION", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
// if (dr == DialogResult.OK)
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
// }
// }
// else
// {
// Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
// }
// }
// }
//}
C# 创建互斥进程(程序) 互斥进程(程序), 简单点说,就是在系统中只能有该程序的一个实例运行. 现在很多软件都有这功能,如Maxthon 可以设置为"只允许打开一个窗体",还有Bitcomet等. 我也是看到这些软件的这个功能才来研究这个问题的. 要实现程序的互斥,通常有4中方式,下面用 C# 语言来实现:
实现方式一: 使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.C#实现如下:
把program.cs文件里的Main()函数改为如下代码:
static void Main()
{
bool runone;
System.Threading.Mutex run = new System.Threading.Mutex(true, "jiaao_test", out runone);
if (runone)
{
run.ReleaseMutex();
Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{ MessageBox.Show("已经运行了一个实例了。");
}
}
相关文章推荐
- C# 只能运行一个winForm进程
- C# 只能运行一个WinForm进程
- C#winform判断程序已经运,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C#进程间的同步,实现只能运行一个程序的效果
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C#程序双击运行之后,界面不显示,但是在任务管理器有进程(一个winform找bug之旅)
- C# Winform 只能运行一个实例笔记
- C# WINFORM判断程序是否运行,且只能运行一个实例(转)
- C# WINFORM判断程序是否运行,且只能运行一个实例
- c# Winform 如何实现程序只能同时运行一个实例
- C#只能运行一个exe进程
- C# WINFORM判断程序是否运行,且只能运行一个实例
- C# 创建互斥进程(程序只能运行一个实例)
- C# WINFORM判断程序是否运行,且只能运行一个实例
- (小技巧六)只允许一个进程运行winform
- windows进程限制工具,可以限制其它软件只能运行一个
- [WinForm]C# .net防止一个程序(WinForm)重复运行的方法。
- C# 程序只能运行一次(多次运行只能打开同一个程序) 并激活第一个实例,使其获得焦点,并在最前端显示.