Danial
Danial

Reputation: 612

c# keep running application without run

I was trying to implement key-logger and mouse-logger in .net. So I have a class KeyLog and MouseLog. 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.

N.B.: It is a console application.

Now I have another class, PLog(Parent Log), inside that I have :

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

This PLog is called from a Main method. The problem is If I put KeyLog before MouseLog in PLog, MouseLog doesn't work, else KeyLog doesn't work.

I can understand the problem, that it is not getting to the next statement, because Application.Run has already been called. How can I resolve this?

I don't want to use thread as this will keep running as long as the PC runs.

using System.Windows.Forms;

Upvotes: 1

Views: 95

Answers (1)

Michael Kokorin
Michael Kokorin

Reputation: 504

  1. Remove Application.Run from KeyLog and MouseLog and call it after setup hooks for both events
  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? May be it is better to use Thread.Sleep or something which is more suitable for this purpose.

Upvotes: 1

Related Questions