小编nur*_*chi的帖子

发布模式下的C#mutex与调试模式下的行为不同

所以我有以下代码:

...
private static void Main(string[] args)
{
    string file=DateTime.Now.ToFileTime().ToString();
    File.AppendAllText(file, "Mutex\r\n");

    bool CreatedNew;
    Mutex mutex=new Mutex(true, AppDomain.CurrentDomain.FriendlyName, out CreatedNew);

    if(CreatedNew)
    {
        #if DEBUG
        File.AppendAllText(file, "Launching in DEBUG mode\r\n");
        #else
        File.AppendAllText(file, "Launching in RELEASE mode\r\n");
        #endif

        //Program.Launch();
        Program.ProcessArgsAndLaunch(args);
    }
    else
    {
        File.AppendAllText(file, "Handling dupe\r\n");
        Program.HandleDuplicate();
    }
}
...
Run Code Online (Sandbox Code Playgroud)

我在这里检查了无数文章,其他网站没有运气.

基本上,代码检查应用程序的运行实例,如果有,则切换到正在运行的实例的主窗口.如果没有,它会启动应用程序.

Debug模式下,它都按预期工作,当我将配置切换到时,问题就开始了Release:应用程序始终启动(Mutex似乎什么都不做).

我添加了条件编译的转储,显示应用程序在哪种模式下启动,输出根据配置进行更改,但遗憾的是,应用程序的行为也是如此.

这可能是一个,race condition但我不确定.

如果需要,将发布更多代码.

谢谢.

c# debugging mutex release

6
推荐指数
2
解决办法
835
查看次数

结构化类和反向隐式转换

假设我有一个结构和一个具有相同成员的类:

using System;

class app
{
    static void Main()
    {
        foo f = new foo() { a  = 4, b = 7 };
        bar b = f;
        Console.WriteLine(b.a);
        Console.ReadKey();
    }

    struct foo
    {
        public int a { get; set; }
        public uint b { get; set; }
    }

    class bar
    {
        public int a { get; set; }
        public uint b { get; set; }

        public static implicit operator foo(bar b)
        {
            return b;
        }

        public static implicit operator bar(foo …
Run Code Online (Sandbox Code Playgroud)

c# implicit-conversion

0
推荐指数
1
解决办法
53
查看次数

标签 统计

c# ×2

debugging ×1

implicit-conversion ×1

mutex ×1

release ×1