C# 坠入老鼠钩

C# 坠入老鼠钩,c#,mousekeyhook,C#,Mousekeyhook,有人能看看我的C#source有崩溃问题吗?该程序应该忽略鼠标有时发送的不必要的双击,它可以工作,但使用该程序一段时间后就会崩溃 崩溃的线路: Application.Run(new TheContext()); 这是错误代码: An unhandled exception of type 'System.NullReferenceException' occurred in Gma.System.MouseKeyHook.dll 我正在使用Visual studio社区2017 资料来源:

有人能看看我的C#source有崩溃问题吗?该程序应该忽略鼠标有时发送的不必要的双击,它可以工作,但使用该程序一段时间后就会崩溃

崩溃的线路:

Application.Run(new TheContext());
这是错误代码:

An unhandled exception of type 'System.NullReferenceException' occurred in Gma.System.MouseKeyHook.dll
我正在使用Visual studio社区2017

资料来源:

program.cs:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用系统线程;
名称空间鼠标器
{
静态类程序
{
静态互斥pmu;
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
//Console.WriteLine(“你好”);
尝试
{
Mutex.OpenExisting(“mousefix”);
MessageBox.Show(“MouseFixer已在运行”、“”、MessageBoxButtons.OK、MessageBoxIcon.Stop);
返回;
}
抓住
{
pmu=新的互斥体(true,“mousefix”);
}
运行(newtheContext());
}
}
}
context.cs:

使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用Gma.System.MouseKeyHook;
使用系统诊断;
名称空间鼠标器
{
公共类上下文:应用程序上下文
{
// https://stackoverflow.com/questions/30061813/intercept-mouse-click
字符串生成器日志文本;
长时间;
长尾鼠;
bool ignoreClick=false;
void writeLog(字符串消息)
{
追加(msg+Environment.NewLine);
AppendAllText(“log.txt”,logText.ToString());
logText.Clear();
}
bool errorshow=false;
void errorMsg(字符串str)
{
如果(!显示错误)
MessageBox.Show(str);
ErrorShowed=true;
}
long getTime()
{
return DateTimeOffset.Now.tounixtimemillides();
}
公共上下文()
{
Application.ApplicationExit+=新的EventHandler(this.OnExit);
logText=新的StringBuilder();
lastClickTime=getTime();
lastMouseUp=getTime();
Hook.GlobalEvents().MouseDownExt+=async(发送方,e)=>
{
if(e.Button==MouseButtons.Left)
{
//e.已处理=正确;
//writeLog(“处理点击下!”+e.Delta);
long lmu=(getTime()-lastMouseUp);
如果(lmu<10)
{
Debug.WriteLine(“点击太快-忽略”+(getTime()-lastMouseUp)+Environment.NewLine);
e、 已处理=正确;
ignoreClick=true;
}
long lct=getTime()-lastClickTime;
lastClickTime=getTime();
Debug.WriteLine(“MouseDOWN”+lct+(“+lmu+”)”+Environment.NewLine);
}
};
Hook.GlobalEvents().MouseUpExt+=异步(发送方,e)=>
{
if(e.Button==MouseButtons.Left)
{
如果(!忽略单击)
{
//e.已处理=正确;
//writeLog(“处理点击!”+e.Delta);
long lct=getTime()-lastClickTime;
lastClickTime=getTime();
Debug.WriteLine(“MouseUP”+lct+Environment.NewLine);
lastMouseUp=getTime();
}
其他的
{
Debug.WriteLine(“忽略单击”+Environment.NewLine);
e、 已处理=正确;
ignoreClick=false;
}
}
};
}
私有void OnExit(对象发送方,事件参数)
{
//AppendAllText(“log.txt”,logText.ToString());
}
}
}

很遗憾,这个库很容易出错。使用Hook.GlobalEvents()是问题的开始,程序没有对实现该钩子的类对象的活动引用。下一次垃圾回收会将其销毁,现在当操作系统进行回调时,您将获得AccessViolationException。CLR将其转换为NRE。注意在作者的文章中使用了m_GlobalHook,这是GC需要看到的重要参考。不要犹豫,让它成为静态的。很遗憾,这个库很容易出错。使用Hook.GlobalEvents()是问题的开始,程序没有对实现该钩子的类对象的活动引用。下一次垃圾回收会将其销毁,现在当操作系统进行回调时,您将获得AccessViolationException。CLR将其转换为NRE。注意在作者的文章中使用了m_GlobalHook,这是GC需要看到的重要参考。不要犹豫,让它静止。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace MouseFixer
{
    static class Program
    {

        static Mutex pmu;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {

            // Console.WriteLine("Hello");

            try
            {
                Mutex.OpenExisting("MouseFixer");

                MessageBox.Show("MouseFixer is already running", "", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }
            catch
            {
                pmu = new Mutex(true, "MouseFixer");
            }

            Application.Run(new TheContext());

        }


    }
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using Gma.System.MouseKeyHook;

using System.Diagnostics;


namespace MouseFixer
{
    public class TheContext : ApplicationContext
    {

        // https://stackoverflow.com/questions/30061813/intercept-mouse-click

        StringBuilder logText;

        long lastClickTime;
        long lastMouseUp;
        bool ignoreClick = false;

        void writeLog(string msg)
        {
            logText.Append(msg + Environment.NewLine);
            File.AppendAllText("log.txt", logText.ToString());
            logText.Clear();
        }

        bool errorShown = false;
        void errorMsg(string str)
        {
            if(!errorShown)
            MessageBox.Show(str);

            errorShown = true;
        }

        long getTime()
        {
           return DateTimeOffset.Now.ToUnixTimeMilliseconds();
        }

        public TheContext()
        {

            Application.ApplicationExit += new EventHandler(this.OnExit);

            logText = new StringBuilder();

            lastClickTime = getTime();
            lastMouseUp = getTime();

            Hook.GlobalEvents().MouseDownExt += async (sender, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    //  e.Handled = true;

                    //  writeLog("Handling click DOWN! " + e.Delta);

                    long lmu = (getTime() - lastMouseUp);

                    if (lmu < 10)
                    {
                        Debug.WriteLine("Too fast click - ignoring " + (getTime() - lastMouseUp) + Environment.NewLine);
                        e.Handled = true;
                        ignoreClick = true;
                    }

                    long lct = getTime() - lastClickTime;

                    lastClickTime = getTime();

                    Debug.WriteLine("MouseDOWN " + lct + " ( " + lmu + " ) " + Environment.NewLine);
                }
            };

            Hook.GlobalEvents().MouseUpExt += async (sender, e) =>
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (!ignoreClick)
                    {

                        //  e.Handled = true;

                        //    writeLog("Handling click UP! " + e.Delta);

                        long lct = getTime() - lastClickTime;

                        lastClickTime = getTime();

                        Debug.WriteLine("MouseUP " + lct + Environment.NewLine);

                        lastMouseUp = getTime();


                    }
                    else
                    {
                        Debug.WriteLine("Ignoring click " + Environment.NewLine);

                        e.Handled = true;
                        ignoreClick = false;
                    }
                }
            };


                }

        private void OnExit(object sender, EventArgs e)
        {
          //  File.AppendAllText("log.txt", logText.ToString());
        }


    }

}