在.NET(而不是Windows窗体或控制台)下使用C#和WPF ,创建只能作为单个实例运行的应用程序的正确方法是什么?
我知道这与一些叫做互斥的神秘事物有关,我很少能找到一个不愿意停下来解释其中一个是什么的人.
代码还需要通知已经运行的实例用户尝试启动第二个,并且如果存在任何命令行参数,也可以传递.
我听说过这些与并发编程有关的词,但它们之间的区别是什么?
在.NET中,防止应用程序的多个实例同时运行的最佳方法是什么?如果没有"最佳"技术,每种解决方案需要考虑哪些注意事项?
我正在使用此代码阻止我的程序的第二个实例同时运行,是否安全?
Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx");
if (appSingleton.WaitOne(0, false)) {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
appSingleton.Close();
} else {
MessageBox.Show("Sorry, only one instance of MyApp is allowed.");
}
Run Code Online (Sandbox Code Playgroud)
我担心如果有什么东西抛出一个异常并且应用程序崩溃了Mutex仍然会被保留.真的吗?
如何检查我的C#Windows应用程序是否正在运行?
我知道我可以检查进程名称,但如果exe更改,则可以更改名称.
有没有办法让哈希键或其他东西使我的应用程序独一无二?
我使用ClickOnce安装部署了我的C#WinForms应用程序.一切正常(经过大量工作):),但现在我遇到了一个问题:
每当我单击"开始"菜单中的应用程序快捷方式时,都会启动一个新实例.我需要避免这种情况.
我该怎么做才能防止多次发布?
为了只允许运行应用程序的单个实例,我正在使用互斥锁.代码如下.这是正确的方法吗?代码中是否有任何缺陷?
当用户第二次尝试打开应用程序时,如何显示已在运行的应用程序.目前(在下面的代码中),我只是显示另一个实例已在运行的消息.
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) 在我的程序中,我使用运行对象表(ROT)来确保只运行一个程序实例.由于我从一位不幸离开公司的开发人员那里"继承"了这段代码,我是解决问题的穷人.代码工作正常,但我们有3个客户(39,000个)将获得AccessDeniedException.每个客户都以用户模式运行软件.
有什么建议可能是错的吗?
bool retVal = false;
IMoniker[] arrMoniker = new IMoniker[1];
IBindCtx bindCtx = null;
string displayName;
int hResult;
int mkSys;
Guid clsidRot;
bool guidCompare = false;
IntPtr number = IntPtr.Zero;
moreObjectsListed = false;
objectFromRot = null;
try
{
// check the objects in the running object table for fitting the specified class id
while ((retVal == false) && (0 == enumMoniker.Next(1, arrMoniker, number)))
{
hResult = CreateBindCtx(0, out bindCtx);
if (hResult == 0)
{
arrMoniker[0].IsSystemMoniker(out mkSys);
if …Run Code Online (Sandbox Code Playgroud) 我的问题是关于URL协议.
我已经注册了一个名为mcm的URL协议,但我注意到每次从任何Web浏览器运行它都会创建一个新的应用程序实例.有没有办法在已经运行的实例中处理协议请求?
例如,当uTorrent使用torrent协议时,它会立即处理请求,而无需再次运行应用程序.我真的找不到任何有趣的东西,所以我在这里问...
这是我用来注册协议的代码:
private static void RegisterUrlProtocol()
{
UnregisterUrlProtocol();
RegistryKey rKey = Registry.ClassesRoot.OpenSubKey(UrlProtocol, true);
if (rKey == null)
{
rKey = Registry.ClassesRoot.CreateSubKey(UrlProtocol);
rKey.SetValue("", "URL: MazCraft Protocol");
rKey.SetValue("URL Protocol", "");
rKey = rKey.CreateSubKey(@"shell\open\command");
rKey.SetValue("", "\"" + Application.ExecutablePath + "\" %1");
}
if (rKey != null)
{
rKey.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
以及读取参数的代码:
private static bool CheckForProtocolMessage()
{
string[] arguments = Environment.GetCommandLineArgs();
if (arguments.Length > 1)
{
string[] args = arguments[1].Split(':');
args[1] = args[1].Replace("//", "");
if (args[0].Trim().ToUpper() == "MCM" …Run Code Online (Sandbox Code Playgroud) c# ×9
mutex ×5
.net ×2
concurrency ×2
winforms ×2
clickonce ×1
deployment ×1
localization ×1
locking ×1
protocols ×1
semaphore ×1
url ×1
wpf ×1