怎么判断本程序在系统进程里只有一个,如果已经有了就不启动?
解决方案 »
- |ZYCWPF| 在WPF当中,如何给Gird的每一行,每一列统一设置Padding呢、?
- img控件问题,怎么选择给定的图片
- C# 读取CSV文件中的某一行
- ListView控件和DataGridView控件有什么区别?
- 为什么sqlconnection SqlDataAdapter DataSet关闭后句柄和内存还不释放
- 使用C#如何改写C++程序?
- 关于文件夹伪装。
- 探讨下软件行业
- 如何防止用户直接访问其他页面,而绕过登录页面!
- 请问C#中能创建对象数组吗?
- 一个非常初级的问题,谢谢!--关于C#创建和编译.cs文件
- 一个WEB系统(C#。NET),我想在打开登录页面时就检验某台电脑是否有权限访问本系统,能不能通过检验该客户端电脑上是否存在某文件,若存
using System.Diagnostics;
using System.Reflection;
...
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;
}
{
Process proce = myClass.RunningInstance();
if(proce == null)
{
Application.Run(new Form1());
}
}
In Visual Basic 6.0, the PrevInstance property of the App object was used to determine if a previous instance of an application was running. There is no equivalent for this property in Visual Basic .NET; however, the following code can be used to test for a previous instance:' Visual Basic .NET
Function PrevInstance() As Boolean
If Ubound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
Return True
Else
Return False
End If
End Function
Note The behavior is slightly different than that of Visual Basic 6.0. In Visual Basic 6.0, PrevInstance returned true only if the full path and file name were exactly the same; in Visual Basic .NET, this function will return true for two instances started from different paths. In addition, PrevInstance would never return true for the first instance of an application; in Visual Basic .NET, once a second instance is loaded, the first instance will also return true.
See Also
App Object Changes in Visual Basic .NET
Public Sub Main()
If PrevInstance() Then Exit Sub ' continue with your application
UserName = Environ("UserName")
ComputerName = Environ("COMPUTERNAME") End Sub
static void Main()
{
//声明互斥体。
Mutex mutex = new Mutex(false, "ThisShouldOnlyRunOnce");
//判断互斥体是否使用中。
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new Form1());
else
MessageBox.Show("应用程序已经启动!"); //Application.EnableVisualStyles();
//Application.Run(new Form1());//窗体名称
}
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("應用程序已啟動!");
bool Running = !mutex.WaitOne(0, false);
if (! Running)
Application.Run(new FormMain());
else
MessageBox.Show("應用程序已啟動!");