//判断是否已经存在一个exe
是要写在main函数里面的
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "yourexe", out createdNew);
if (! createdNew)
{
MessageBox.Show("Only one exe is allowed at a time.");
return;
}
Application.Run(new Start());
GC.KeepAlive(m);
}上面的要添加using System.Threading;
Mutex class是用于进程同步的
是要写在main函数里面的
[STAThread]
static void Main()
{
bool createdNew;
Mutex m = new Mutex(true, "yourexe", out createdNew);
if (! createdNew)
{
MessageBox.Show("Only one exe is allowed at a time.");
return;
}
Application.Run(new Start());
GC.KeepAlive(m);
}上面的要添加using System.Threading;
Mutex class是用于进程同步的
解决方案 »
- 用C# 编写程序...急急急
- 麻烦各位高手帮忙分析一个登陆页面
- 高分求解:如何把listview中的数据导出,如excel, word,....等.
- 请问用C#跟SQL SERVER2000 开发的数据库系统怎么做成安装文件?
- 请c#高手来帮小弟一下
- ****子类中的BASE问题*****
- windows2003不能正常启动,紧急求助!顺便给大家拜个晚年:)
- EnyimMemcached 谁用过 编译通过启动和 调式出问题.
- 数组怎么从第一个窗体传到另一个窗体中去
- 各位高手,请问一下怎么实现“查找下一个”的功能???
- 怎样用API函数来禁止一个窗口多次打开?
- 我想在程序的任何地方控制listview中的显示,大家说能实现吗?
Process[] processes = Process.GetProcessesByName(yourProcessesName);
if (processes.Length >= 2 )
{
//处理多次运行
......
return;
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name
foreach (Process process in processes)
{
//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;
}
}
}
//No other instance was found, return null.
return null;
} [VB.NET]
Public Shared Function RunningInstance() As Process
Dim current As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName) 'Loop through the running processes in with the same name
Dim process As Process
For Each process In processes
'Ignore the current process
If process.Id <> current.Id Then
'Make sure that the process is running from the exe file.
If [Assembly].GetExecutingAssembly().Location.Replace("/", "\") = current.MainModule.FileName Then
'Return the other process instance.
Return process
End If
End If
Next process
'No other instance was found, return null.
Return Nothing
End Function 'RunningInstance
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection; public class OneInstnace
{
[STAThread]
public static void Main()
{
//Get the running instance.
Process instance = RunningInstance();
if (instance == null)
{
//There isn't another instance, show our form.
Application.Run (new Form());
}
else
{
//There is another instance of this process.
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName); //Loop through the running processes in with the same name
foreach (Process process in processes)
{
//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;
}
}
} //No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
{
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL); //Set the real intance to 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;
}
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
// 防止多次执行
Process[] processes = Process.GetProcessesByName("RCL");
if (processes.Length >= 2 )
{
MessageBox.Show("程序已经执行!","提醒",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
else
Application.Run(new frmMain());
}
VB.NET:if (UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0)
{
MessageBox.Show("already started");
}