请问怎样只启动一个实例进程? 我开发C#程序中,需要在启动系统时,启动这个程序,但是现在有个问题如果多用户登录这个系统,程序会启动多次,如果启动多次程序就出问题,有什么方法实现,只启动一个进程 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 嗯,最近刚刚弄了一个Main函数[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); webscreenshot wb = new webscreenshot(); Process current = Process.GetCurrentProcess(); bool newinstance = true; 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) { //返回另一个例程实例 current = process; newinstance = false; break; } } } if (newinstance) { Application.Run(wb); } else { ShowWindowAsync(current.MainWindowHandle, 1); //设置真实例程为foreground window SetForegroundWindow(current.MainWindowHandle); } }引入这两个API函数[DllImport("User32.dll")] private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); [DllImport("User32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); }就是这样 求SharpDevelop的代码格式化快捷键,以及Visual studio代码格式化快捷键 C#怎样加速文件读取 SqlDataAdapter的update()方法为什么不能进行删除和更新??? 菜鸟的委托问题! 用C# 开发 soap 的客户端 可以吗,怎么实现 利用c#将sqlserver中的数据导入excel的问题 Session用起来确实挺麻烦啊。。。哎。。。。。。 ????智能开发的问题???? StreamReader读文件问题(急!在线等!马上给分:)虽然分很少!) C#中子窗口对父窗口中属性的访问 文件正由另一个进程访问,因此无法访问改文件!! c#创建结构数组的问题
Main函数
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
webscreenshot wb = new webscreenshot();
Process current = Process.GetCurrentProcess();
bool newinstance = true;
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)
{
//返回另一个例程实例
current = process;
newinstance = false;
break;
}
}
}
if (newinstance)
{
Application.Run(wb);
}
else
{
ShowWindowAsync(current.MainWindowHandle, 1); //设置真实例程为foreground window
SetForegroundWindow(current.MainWindowHandle);
}
}
引入这两个API函数
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync( IntPtr hWnd, int cmdShow); [DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
}就是这样