gpt4 book ai didi

c# - 代码在 Release模式下工作,但在调试配置中抛出错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:27 25 4
gpt4 key购买 nike

我将我从 @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!");
}
}
}

最佳答案

After running the app in Debug configuration, it crashes [...] But if ran in Release configuration it works without errors.

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

及时编译器和一般编译器优化喜欢删除死代码。然而,它们在发布和 Debug模式之间的优化非常不同。在 Debug模式下主要是“积极性低得多”。因此,可能在 Release 中作为“死代码”删除的代码可能仍处于 Debug模式。

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

关于c# - 代码在 Release模式下工作,但在调试配置中抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58944259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com