MouseKeyHook 'CallbackOnCollectedDelegate' 问题

Posted

技术标签:

【中文标题】MouseKeyHook \'CallbackOnCollectedDelegate\' 问题【英文标题】:MouseKeyHook 'CallbackOnCollectedDelegate' IssueMouseKeyHook 'CallbackOnCollectedDelegate' 问题 【发布时间】:2019-04-23 20:58:29 【问题描述】:

我正在使用 Gma.System.MouseKeyHook 并收到以下异常:

Managed Debugging Assistant 'CallbackOnCollectedDelegate' Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage collected delegate of type 'Gma.System.MouseKeyHook!Gma.System.MouseKeyHook.WinApi.HookProcedure::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called.'

我已尝试处理函数调用和订阅。但是,问题仍然存在。我也尝试多次运行它,偶尔它也会给出“NullReferenceException”。这也让我很困惑,也许这些问题是相关的。

using Gma.System.MouseKeyHook;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;



// Uses a MouseKeyHook Library license at 
https://github.com/gmamaladze/globalmousekeyhook/blob/master/LICENSE.txt

namespace TransparentClickTest 
public partial class Form1 : Form 


    public Form1() 
        //GC.TryStartNoGCRegion(100);
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        BackColor = Color.Red;
        TransparencyKey = Color.Red;
        InitializeComponent();

        OnDown();            

    
    protected virtual void OnUp() 
        Hook.GlobalEvents().MouseUp += (sender, e) => 
            try 
                label1.Text = "Mouse Up!!!";
                Hook.GlobalEvents().Dispose();
                OnDown();
            
            catch(Exception e2) 
                Hook.GlobalEvents().Dispose();
                OnDown();                   
            
        ;
    

    protected virtual void OnDown() 
        Hook.GlobalEvents().MouseDown += (sender, e) => 
            try 
                label1.Text = $"Mouse e.Button Down at e.X, e.Y";
                Opacity = 1;
                Hook.GlobalEvents().Dispose();
                OnUp();
            
            catch(Exception e1) 
                Hook.GlobalEvents().Dispose();
                OnUp();
            
        ;
    

    private void PictureBox1_Click(object sender, EventArgs e) 

    

    private void Label1_Click(object sender, EventArgs e) 

    



【问题讨论】:

【参考方案1】:
    添加:private static IKeyboardMouseEvents HookEvents = null; 使用 HookEvents.MouseDown 替换 Hook.GlobalEvents().MouseDown

【讨论】:

感谢您提供这些替代解决方案。我要解决的是添加额外的 Subscribe() 和 Unsubscribe() 方法,并在其中处理钩子。

以上是关于MouseKeyHook 'CallbackOnCollectedDelegate' 问题的主要内容,如果未能解决你的问题,请参考以下文章

WPF 使用 mousekeyhook 库计算点击次数 - LoaderLock 错误

C# MouseKeyHook:某些键 + 修饰符组合不起作用

如何在我的情况下导入库 MouseKeyHook?

通过 mousekeyhook C# 检测按键的绝对最小代码

鼠标键挂钩 - 键侦听器未触发

等效于使用 c# wpf 的 IsDisposed [重复]