Mutex类被误解了,而Global mutex则更是如此.
在创建全局互斥锁时使用什么是好的,安全的模式?
一个会起作用的
为了只允许运行应用程序的单个实例,我正在使用互斥锁.代码如下.这是正确的方法吗?代码中是否有任何缺陷?
当用户第二次尝试打开应用程序时,如何显示已在运行的应用程序.目前(在下面的代码中),我只是显示另一个实例已在运行的消息.
static void Main(string[] args)
{
Mutex _mut = null;
try
{
_mut = Mutex.OpenExisting(AppDomain.CurrentDomain.FriendlyName);
}
catch
{
//handler to be written
}
if (_mut == null)
{
_mut = new Mutex(false, AppDomain.CurrentDomain.FriendlyName);
}
else
{
_mut.Close();
MessageBox.Show("Instance already running");
}
}
Run Code Online (Sandbox Code Playgroud) 我只是想检查是否有任何所需的应用程序已经运行
例如,假设我有VLC或iTunes或任何Windows应用程序,那么如果它运行与否,我怎么能通过c#代码弄清楚它.
我试着这样做:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using DannyGeneral;
namespace mws
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
if (IsApplicationAlreadyRunning() == true)
{
MessageBox.Show("The application is already running");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
catch (Exception err)
{
Logger.Write("error " + err.ToString());
}
}
static bool IsApplicationAlreadyRunning()
{
string proc = Process.GetCurrentProcess().ProcessName;
Process[] processes = Process.GetProcessesByName(proc); …Run Code Online (Sandbox Code Playgroud) 我需要构建一个小型C#应用程序来测量可定义应用程序的启动时间.我找到了这个免费工具:
http://www.passmark.com/products/apptimer.htm
这正是我需要的,但我被要求创建一个更具公司特定的内部版本,因为我们需要数据库日志记录和其他一些东西.
我理解如何复制除"输入空闲"之外的所有功能.我试图搜索这个术语,但我没有找到任何有用的参考.任何人都可以指出我正确的方向检查应用程序"输入空闲"状态?