简体   繁体   English

c#继续运行应用程序而不运行

[英]c# keep running application without run

I was trying to implement key-logger and mouse-logger in .net.我试图在 .net 中实现键盘记录器和鼠标记录器。 So I have a class KeyLog and MouseLog .所以我有一个KeyLogMouseLog类。 Inside both class's constructor, I have this same logic :在两个类的构造函数中,我有相同的逻辑:

MouseLog:鼠标日志:

 public MouseLog()
        {
            _hookID = SetHook(_proc);
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }

KeyLog :按键日志:

public KeyLog()
        {
            _hookID = SetHook(_proc);
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }

I'm using Application.Run to keep running the application.我正在使用Application.Run来继续运行应用程序。

NB: It is a console application.注意:它是一个控制台应用程序。

Now I have another class, PLog (Parent Log), inside that I have :现在我有另一个类, PLog (父日志),里面有:

public PLog()
{
      KeyLog();
      MouseLog();
}

This PLog is called from a Main method.这个PLog是从Main方法调用的。 The problem is If I put KeyLog before MouseLog in PLog , MouseLog doesn't work, else KeyLog doesn't work.问题是如果我把KeyLog之前MouseLogPLogMouseLog不能正常工作,否则KeyLog不起作用。

I can understand the problem, that it is not getting to the next statement, because Application.Run has already been called.我可以理解这个问题,它没有进入下一个语句,因为Application.Run已经被调用。 How can I resolve this?我该如何解决这个问题?

I don't want to use thread as this will keep running as long as the PC runs.我不想使用线程,因为只要 PC 运行,它就会一直运行。

using System.Windows.Forms;使用 System.Windows.Forms;

  1. Remove Application.Run from KeyLog and MouseLog and call it after setup hooks for both events从 KeyLog 和 MouseLog 中删除 Application.Run 并在为两个事件设置挂钩后调用它
  2. Consider to make unhook actions some events from application - like application closing or so.考虑对来自应用程序的某些事件进行解钩操作 - 例如应用程序关闭等。
  3. It is better to use threads for such operations此类操作最好使用线程
  4. If this is console application - why do you use Windows Forms to keep it running?如果这是控制台应用程序 - 为什么要使用 Windows 窗体来保持它运行? May be it is better to use Thread.Sleep or something which is more suitable for this purpose.可能最好使用 Thread.Sleep 或更适合此目的的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM