如何实现只让一个程序运行一次,在它运行时,不允许再启动一个新的进程。

windz 2004-07-08 05:03:30
如何实现只让一个程序运行一次,在它运行时,不允许再启动一个新的进程。

我的系统是WINDOWS2003,用的环境是。NET 2003, VC#
...全文
891 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
frankyshu 2005-08-08
  • 打赏
  • 举报
回复
mark
kqh0319 2005-05-11
  • 打赏
  • 举报
回复
正需要........
冰河绝恋 2005-05-11
  • 打赏
  • 举报
回复
hanbinghai(海宁) ---ok
Kevia 2005-04-27
  • 打赏
  • 举报
回复
学习
xiaomatian 2005-04-11
  • 打赏
  • 举报
回复
可以使用互斥体Mutex类型完成此功能。见如下代码:
[STAThread]
public static void Main(string[] args)
{
//声明互斥体。
Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中。
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormLogin());
else
MessageBox.Show("应用程序已经启动!");
}
kongrh 2005-04-11
  • 打赏
  • 举报
回复
好办法
shooper 2005-04-11
  • 打赏
  • 举报
回复
mark
keiven 2005-03-24
  • 打赏
  • 举报
回复
mark一下
ajou 2004-09-15
  • 打赏
  • 举报
回复
经测试chinawn(chinawin) 的方法在启动应用程序时若鼠标点击比较快,会出现两个启动的同一进程互锁,导致提示两个“该系统已经在运行中”的错误。

因为以前我也是这么做的。
chinawn 2004-09-07
  • 打赏
  • 举报
回复
[STAThread]
static void Main()
{

int ProceedingCount = 0;
Process[] ProceddingCon = Process.GetProcesses();
foreach(Process IsProcedding in ProceddingCon) {
if(IsProcedding.ProcessName == Process.GetCurrentProcess().ProcessName) {
ProceedingCount += 1;
}
}
if(ProceedingCount > 1) {
MessageBox.Show("该系统已经在运行中。","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else {
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new MainFrm());
}
}
hanbinghai 2004-08-03
  • 打赏
  • 举报
回复
Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中。
bool Running = !mutex.WaitOne(0, false);
if (! Running)
{
Application.Run(new Form1());
}
else
{
MessageBox.Show("应用程序已经启动!");
}
langmafeng 2004-08-03
  • 打赏
  • 举报
回复
学习
kfy0002 2004-08-03
  • 打赏
  • 举报
回复
up
123456754321 2004-08-03
  • 打赏
  • 举报
回复
,
ClampHammer 2004-08-03
  • 打赏
  • 举报
回复
收藏先,不错
daou101 2004-08-03
  • 打赏
  • 举报
回复
已经有这么多回答了,我说点吧:

核心就是在系统中注册一个互斥变量,并使用该互斥体控制应用程序的加载。
dahuzizyd 2004-08-03
  • 打赏
  • 举报
回复
我收藏的一个:
http://blog.csdn.net/dahuzizyd/articles/55411.aspx
小贵子88 2004-07-08
  • 打赏
  • 举报
回复
又发一次

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

public class OneInstnace
{
[STAThread]
public static void Main()
{
//得到正在运行的例程
Process instance = RunningInstance();
if (instance == null)
{
//如果没有其它例程,就新建一个窗体
Application.Run (new Form());
}
else
{
//处理发现的例程
HandleRunningInstance(instance);
}
}
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;
}

public static void HandleRunningInstance(Process instance)
{
//确保窗口没有被最小化或最大化
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);

//设置真实例程为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;
}

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧