C#建立了一个windows程序,如何防止多个程序实例运行?有什么技巧吗? C#建立了一个windows程序,如何防止多个程序实例运行?有什么技巧吗? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可以在Main函数中如下操作:[STAThread]static void Main(){ System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName); if (ps.Length <= 1) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); }} 收藏学习了 借此宝地请教个问题:DataGridView控件的每一行记录可否有子行集合(数据来自另外一个相关表),这样形成一个分级,方便浏览.我想效果跟treeview差不多,只是要展示多列记录 Application.SetCompatibleTextRenderingDefault(false);这句换编译没通过,注释掉后达到同样效果 呵呵,我用的VS2005,Application.SetCompatibleTextRenderingDefault(false);是在VS2005中新增的方法.用2003的话就不用了. 在main函数中加入如下bool bCreatedNew;Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );if( bCreatedNew )Application.Run(new Form1()); static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); #region Check the application whether running Process process = RunningInstance(); if (process != null) { MessageBox.Show("This application is already running.", ResourceString.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } #endregion Application.Run(new MainForm()); } //不允许有两个程序同时启动 public static Process RunningInstance() { Process current = Process.GetCurrentProcess(); Process[] processes = Process.GetProcessesByName(current.ProcessName); //遍历正在有相同名字运行的例程 foreach (Process process in processes) { //忽略现有的例程 if (process.Id != current.Id) { //确保例程从EXE文件运行 if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { //返回另一个例程实例 return process; } } } //没有其它的例程,返回Null return null; } 我在这篇文章中介绍了三种实现方式,http://blog.csdn.net/zhzuo/archive/2006/06/30/857405.aspxhttp://blog.csdn.net/zhzuo/archive/2006/07/04/874745.aspx 在main函数中加入如下bool bCreatedNew;Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );if( bCreatedNew )Application.Run(new Form1());===============================================================我就用这个的,还有一个是检测是否存在同路径下的同进程名的方法,但是效率比较低,而且麻烦 回复人:junsheng(月生) ( 一级(初级)) 信誉:99 2007-1-19 15:19:37 得分:0在main函数中加入如下bool bCreatedNew;Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );if( bCreatedNew )Application.Run(new Form1());------------------->>>>以上方法可行,也最簡單,樓主可以使用. using System.Threading;using System.Reflection;using System.Diagnostics; Process[] exe = System.Diagnostics.Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name); if (exe.Length > 1) { MessageBox.Show("程序已运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } C#事务TransactionScope问题 C#的direct编程可行吗? 用C# 怎样实现windows 自动关机 彻底晕倒 重新开帖 关于静态构造函数 asp.net日期加减计算 这样定义一个派生类,怎么出错? SQL问题 将yuv420格式的数据在picturebox中显示出来 C# 字段声明写在什么位置? datagrid问题:中文字段名 点击按钮出错,但是跟踪不到错误? 如何在.net1 中实现对(c#代码 循环插入)的事务~跪求
[STAThread]
static void Main()
{
System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName);
if (ps.Length <= 1)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
借此宝地请教个问题:
DataGridView控件的每一行记录可否有子行集合(数据来自另外一个相关表),这样形成一个分级,方便浏览.我想效果跟treeview差不多,只是要展示多列记录
这句换编译没通过,注释掉后达到同样效果
Application.SetCompatibleTextRenderingDefault(false);
是在VS2005中新增的方法.
用2003的话就不用了.
bool bCreatedNew;
Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
if( bCreatedNew )
Application.Run(new Form1());
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#region Check the application whether running
Process process = RunningInstance();
if (process != null)
{
MessageBox.Show("This application is already running.", ResourceString.AppTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
#endregion Application.Run(new MainForm());
}
//不允许有两个程序同时启动
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//遍历正在有相同名字运行的例程
foreach (Process process in processes)
{
//忽略现有的例程
if (process.Id != current.Id)
{
//确保例程从EXE文件运行
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
current.MainModule.FileName)
{
//返回另一个例程实例
return process;
}
}
}
//没有其它的例程,返回Null
return null;
}
http://blog.csdn.net/zhzuo/archive/2006/06/30/857405.aspx
http://blog.csdn.net/zhzuo/archive/2006/07/04/874745.aspx
bool bCreatedNew;
Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
if( bCreatedNew )
Application.Run(new Form1());===============================================================
我就用这个的,还有一个是检测是否存在同路径下的同进程名的方法,但是效率比较低,而且麻烦
在main函数中加入如下
bool bCreatedNew;
Mutex m =new Mutex( false, "你的程序名", out bCreatedNew );
if( bCreatedNew )
Application.Run(new Form1());------------------->>>>
以上方法可行,也最簡單,樓主可以使用.
using System.Reflection;
using System.Diagnostics;
Process[] exe = System.Diagnostics.Process.GetProcessesByName(Assembly.GetExecutingAssembly().GetName().Name);
if (exe.Length > 1)
{
MessageBox.Show("程序已运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}