【问题标题】:Code works in Release mode, but throws error in Debug configuration代码在发布模式下工作,但在调试配置中抛出错误
【发布时间】:2020-03-15 13:48:49
【问题描述】:

我整理了从 @Chris Taylor. 获得的系统范围键盘快捷键的代码

我希望它在.NETCoreApp 3.0 中工作,因为根据MS docs,支持System.Windows.Forms.dll

我添加了对位于C:\Windows\Microsoft.NET\Framework64\v4.0.30319System.Windows.Forms.dll 的引用(旧版本也可以)。

此时构建成功,但运行应用程序抛出异常:Could not load file or assembly System.Drawing.Common 所以我安装了一个 nuget System.Drawing.Common。由于异常,再次重复并添加了另一个必需的 nuget System.Configuration.ConfigurationManager

这就是奇怪的事情开始发生的地方:在调试配置中运行应用程序后,它崩溃了:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Windows.Forms.NativeWindow.AdjustWndProcFlagsFromConfig(Int32 wndProcFlags)
   at System.Windows.Forms.NativeWindow.get_WndProcFlags()
   at System.Windows.Forms.NativeWindow.get_WndProcShouldBeDebuggable()
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle, Boolean assignUniqueID)
   at System.Windows.Forms.NativeWindow.AssignHandle(IntPtr handle)
   at System.Windows.Forms.NativeWindow.WindowClass.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

但如果在发布配置中运行,它可以正常工作。如果我选择 »bug« 图标以使用调试器运行(与选择调试配置不同),则相同。我的 IDE 是 Rider,可能是相关的。如果使用调试器不再抛出错误,如何调试?

我将在下面发布我的所有代码,但它并不是真正需要的,因为它是直接从我在上面链接的 Chris Taylor 的答案中复制而来的。谁能告诉我这里发生了什么?为什么 Release 模式可以正常工作,而 Debug 配置却不行?

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;

namespace ConsoleApp10
{
  public static class HotKeyManager
  {
    public static event EventHandler<HotKeyEventArgs> HotKeyPressed;

    public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
    {
      _windowReadyEvent.WaitOne();
      int id = System.Threading.Interlocked.Increment(ref _id);
      _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
      return id;
    }

    public static void UnregisterHotKey(int id)
    {
      _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
    }

    delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
    delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);

    private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
    {      
      RegisterHotKey(hwnd, id, modifiers, key);      
    }

    private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
    {
      UnregisterHotKey(_hwnd, id);
    }    

    private static void OnHotKeyPressed(HotKeyEventArgs e)
    {
      if (HotKeyManager.HotKeyPressed != null)
      {
        HotKeyManager.HotKeyPressed(null, e);
      }
    }

    private static volatile MessageWindow _wnd;
    private static volatile IntPtr _hwnd;
    private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
    static HotKeyManager()
    {
      Thread messageLoop = new Thread(delegate()
        {
          Application.Run(new MessageWindow());
        });
      messageLoop.Name = "MessageLoopThread";
      messageLoop.IsBackground = true;
      messageLoop.Start();      
    }

    private class MessageWindow : Form
    {
      public MessageWindow()
      {
        _wnd = this;
        _hwnd = this.Handle;
        _windowReadyEvent.Set();
      }

      protected override void WndProc(ref Message m)
      {
        if (m.Msg == WM_HOTKEY)
        {
          HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
          HotKeyManager.OnHotKeyPressed(e);
        }

        base.WndProc(ref m);
      }

      protected override void SetVisibleCore(bool value)
      {
        // Ensure the window never becomes visible
        base.SetVisibleCore(false);
      }

      private const int WM_HOTKEY = 0x312;
    }

    [DllImport("user32", SetLastError=true)]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32", SetLastError = true)]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private static int _id = 0;
  }


  public class HotKeyEventArgs : EventArgs
  {
    public readonly Keys Key;
    public readonly KeyModifiers Modifiers;

    public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
    {
      this.Key = key;
      this.Modifiers = modifiers;
    }

    public HotKeyEventArgs(IntPtr hotKeyParam)
    {
      uint param = (uint)hotKeyParam.ToInt64();
      Key = (Keys)((param & 0xffff0000) >> 16);
      Modifiers = (KeyModifiers)(param & 0x0000ffff);
    }
  }

  [Flags]
  public enum KeyModifiers
  {
    Alt = 1,
    Control = 2,
    Shift = 4,
    Windows = 8,
    NoRepeat = 0x4000
  }
}

还有我的主要:

using System;
using System.Windows.Forms;

namespace ConsoleApp10 {
   class Program {
      static void Main(string[] args) {
         Console.WriteLine("Hello World!");
         HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
         HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);
         Console.ReadLine();   
      }
      static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
      {
         Console.WriteLine("Hit me!");
      }
   }
}

【问题讨论】:

  • 导航到输出目录。然后删除 Debug 和 Release 文件夹。然后重试调试和发布版本。如果有任何变化,请告诉我们。
  • 前往bin 文件夹并删除了DebugRelease 文件夹。再次运行它并得到同样的异常。
  • 你在这个方法中有这个代码:UnregisterHotKey(_hwnd, id);private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)——不知道这是不是故意的,但看起来很可疑。
  • 嗯,该属性被称为 WndProcShouldBeDebuggable,所以它在发布版本中工作并不神秘。接下来它会查阅 app.config 文件和代码炸弹。建议您使用 System.Configuration.ConfigurationManager 获得一些 DLL Hell,确保使用 3.0 版本。总的来说,.NETCore v3.0 还没有调试好,特别是因为 winforms 设计器要到 v3.1 才能使用。最好提交bug report
  • 您确定从 Framework64\v4.0... 引用 DLL 是使用 Windows 窗体组件构建 .NET Core 3 应用程序的正确方法吗?您可能需要仔细检查,例如这里:devblogs.microsoft.com/dotnet/net-core-3-for-windows-desktop 和这里:grapecity.com/blogs/winforms-and-dotnet-core-3

标签: c# .net-core


【解决方案1】:

在 Debug 配置中运行应用程序后,它会崩溃 [...] 但如果在 Release 配置中运行,它可以正常工作。

这是一个猜想,但也许你有死代码?

即时编译器和通用编译器优化喜欢删除死代码。然而,它们在发布和调试模式之间的优化非常不同。在调试模式下主要是“积极地少得多”。因此,在 Release 中可能被删除为“死代码”的代码可能仍处于 Debug 模式。

让事情变得更难的是,您还混合了多线程。众所周知,优化在这里会导致问题。事实上,这就是 Volatile Keyword 这样的东西存在的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    相关资源
    最近更新 更多