如何控制应用程序同一时间只能打开一次

zz124 2003-07-07 03:28:13

如何控制用c#编写的Windows应用程序在同一时间只能打开一个
...全文
190 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudixiaocaoren 2003-07-09
  • 打赏
  • 举报
回复
我再给大家贴一个防止程序运行两份的代码
这个例子用一个全局的内核对象防止同一个程序运行两次,
优点是判断速度相当快!

首先定义一个类OneInstance,该类只包含一个静态的公用方法IsFirst
用于判断程序是否是第一次运行。
代码如下:
 
using System;
using System.Runtime.InteropServices;
public abstract class OneInstance
{
/// <summary>
/// 用来判断一个指定的程序是否正在运行
/// </summary>
/// <param name="appId">程序名称,长一点比较好,防止有重复</param>
/// <returns>如果程序是第一次运行返回True,否则返回False</returns>
public static bool IsFirst(string appId)
{
bool ret=false;
if(OpenMutex(0x1F0001,0,appId)==IntPtr.Zero)
{
CreateMutex(IntPtr.Zero,0,appId);
ret=true;
}
return ret;
}

[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr OpenMutex(
uint dwDesiredAccess, // access
int bInheritHandle, // inheritance option
string lpName // object name
);

[DllImport("Kernel32.dll",CharSet=CharSet.Auto)]
private static extern IntPtr CreateMutex(
IntPtr lpMutexAttributes, // SD
int bInitialOwner, // initial owner
string lpName // object name
);
}


然后在主程序的入口方法里加上以下代码
 
[STAThread]
static void Main()
{
if(!OneInstance.IsFirst("My Application Test"))
{
MessageBox.Show("My application is running");
return;
}
Application.Run(new Form1());
}



在你的其它程序里使用只需要将不同的程序标识串传入OneInstance.IsFirst方法
就行了,很方便,大家可以试一试。
wudixiaocaoren 2003-07-09
  • 打赏
  • 举报
回复
限制应用程序两次以上被运行:
[STAThread]
static void Main() {
Process[] myProcesses = Process.GetProcessesByName(Application.ProductName );
if(myProcesses.Length<=1)
Application.Run(new LogOn());
else
MessageBox.Show("程序已经启动!","提示",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
Application.Exit();
}
chinchy 2003-07-07
  • 打赏
  • 举报
回复
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class AppMain
{
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);

private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;

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)
{
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us.

// get our process
Process p=Process.GetCurrentProcess();
int n=0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id==p.Id)
{
// then the other process is at index 1
n=1;
}
// get the window handle
IntPtr hWnd=processes[n].MainWindowHandle;
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
return;
}
// ... continue with your application initialization here.
}
}
chinchy 2003-07-07
  • 打赏
  • 举报
回复
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
class AppMain
{
[DllImport("user32.dll")] private static extern
bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")] private static extern
bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")] private static extern
bool IsIconic(IntPtr hWnd);

private const int SW_HIDE = 0;
private const int SW_SHOWNORMAL = 1;
private const int SW_SHOWMINIMIZED = 2;
private const int SW_SHOWMAXIMIZED = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;

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)
{
// Assume there is our process, which we will terminate,
// and the other process, which we want to bring to the
// foreground. This assumes there are only two processes
// in the processes array, and we need to find out which
// one is NOT us.

// get our process
Process p=Process.GetCurrentProcess();
int n=0; // assume the other process is at index 0
// if this process id is OUR process ID...
if (processes[0].Id==p.Id)
{
// then the other process is at index 1
n=1;
}
// get the window handle
IntPtr hWnd=processes[n].MainWindowHandle;
// if iconic, we need to restore the window
if (IsIconic(hWnd))
{
ShowWindowAsync(hWnd, SW_RESTORE);
}
// bring it to the foreground
SetForegroundWindow(hWnd);
// exit our process
return;
}
// ... continue with your application initialization here.
}
}
superzuoluo 2003-07-07
  • 打赏
  • 举报
回复
假如你的应用程序的名称为MyApp.exe,则将vrvmon改为MyApp。
superzuoluo 2003-07-07
  • 打赏
  • 举报
回复
if(System.Diagnostics.Process.GetProcessesByName("vrvmon").Length > 0)
MessageBox.Show("a");

110,555

社区成员

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

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

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