oblitum / Interception

The Interception API aims to build a portable programming interface that allows one to intercept and control a range of input devices.
http://oblita.com/interception
1.35k stars 269 forks source link

Stop interception_wait #31

Closed dudul closed 8 years ago

dudul commented 8 years ago

Hi, Is there an option to stop the "interception_wait"? My situation is: My app receives a command from another application, which supposed to stop the input hooking. I thought about 2 solutions:

  1. Run the hooking process on another thread and close it when the app receive the stop command - Is it safe?
  2. Use "interception_wait_with_timeout" and check every x seconds (exit the loop if a boolean variable was update) - How it will affect on OS performances?

Please advise, Thanks

oblitum commented 8 years ago

It's not possible to stop interception_wait, it's a blocking API.

  1. Yes, you can use interception from a secondary thread without issues.
  2. Also an option, I've never measured performance, it will also depend how quickly you manage things on your side.
oblitum commented 8 years ago

Ah, regarding 1., if interception_wait is waiting, and you wish to just kill the thread, I'm really not sure how you would proceed, I don't recall about thread killing on Windows. But, terminating a process blocked on interception_wait for example, is fine.

Knufje commented 5 years ago

I am having the same problem and yet have to find a solution. When I use Abort() on the thread that is on InterceptionDriver.WaitWithTimeout(,10000) it still waits for the timeout. But I need to have it instantly exit.

oblitum commented 5 years ago

@Knufje it may be a problem with the wrapper you're using.

Knufje commented 5 years ago

This is what I have at the moment, with using tasks instead of threads again and since I don't know much about Tasks and threads I have spend hours searching, but it seems that I can't exit that wait with just my code. Is there something that I am missing? ` class InputSystem { CancellationTokenSource tokenSource = new CancellationTokenSource();

        void Start()
        {
            Task.Run(() => interceptInput(), tokenSource.Token);
        }

        void Cancel()
        {
            tokenSource.Cancel();
        }

        void interceptInput()
        {
            bool taskAbort = false;
            IntPtr context = InterceptionDriver.CreateContext();

            int deviceId;
            Stroke stroke = new Stroke();
            try
            {
                while (!taskAbort)
                {
                    while (Interceptor.InterceptionDriver.Receive(context, deviceId = InterceptionDriver.WaitWithTimeout(context, 10000), ref stroke, 1) > 0)
                    {
                        // Poll on this property if you have to do
                        // other cleanup before throwing.
                        if (tokenSource.Token.IsCancellationRequested)
                        {
                            taskAbort = true;
                            Form1.DebugMsg("InputSystem", "cancel active keyset.");

                            // Clean up here, then...
                            tokenSource.Token.ThrowIfCancellationRequested();
                        }

                    }
                }
            }
            catch (OperationCanceledException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

`

oblitum commented 5 years ago

@Knufje Sorry but I can't give support for a .NET wrapper I didn't write, you should look for the .NET wrapper's help. It's an independent wrapper, with no official relation to this project.