Use CancellationToken to start/stop thread, add start/stop to MouseTester

pull/76/head
Clive Galway 3 years ago
parent 8f4539ac95
commit e97e4ef12b

@ -44,7 +44,9 @@ namespace AutoHotInterception
private static bool _absoluteMode00Reported;
private readonly Thread _pollThread = new Thread(PollThread);
private readonly Thread _pollThread;
private static bool _pollThreadRunning = false;
private CancellationTokenSource _cancellationToken;
#region Public
@ -517,13 +519,15 @@ namespace AutoHotInterception
private void SetThreadState(bool state)
{
if (state && !_pollThread.IsAlive)
if (state && !_pollThreadRunning)
{
_pollThread.Start();
_cancellationToken = new CancellationTokenSource();
ThreadPool.QueueUserWorkItem(PollThread, _cancellationToken.Token);
}
else if (!state && _pollThread.IsAlive)
else if (!state && _pollThreadRunning)
{
_pollThread.Join();
_cancellationToken.Cancel();
_cancellationToken.Dispose();
}
}
@ -566,11 +570,13 @@ namespace AutoHotInterception
// ScanCode notes: https://www.win.tue.nl/~aeb/linux/kbd/scancodes-1.html
private static void PollThread()
private static void PollThread(object obj)
{
var token = (CancellationToken)obj;
Debug.WriteLine($"AHK| Poll Thread Started");
var stroke = new ManagedWrapper.Stroke();
int i;
while (ManagedWrapper.Receive(_deviceContext, i = ManagedWrapper.Wait(_deviceContext), ref stroke, 1) > 0)
while (!token.IsCancellationRequested && ManagedWrapper.Receive(_deviceContext, i = ManagedWrapper.Wait(_deviceContext), ref stroke, 1) > 0)
{
if (i < 11)
{
@ -883,6 +889,7 @@ namespace AutoHotInterception
//Debug.WriteLine($"AHK| ");
}
}
Debug.WriteLine($"AHK| Poll Thread Ended");
}
internal class MappingOptions

@ -1,52 +1,77 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using AutoHotInterception;
namespace TestApp
{
public class MouseTester
{
private bool _subscribed = false;
private const string MouseHandle = "HID\\VID_046D&PID_C00C&REV_0620"; // Logitech USB
private readonly Manager _im = new Manager();
private readonly int _devId;
private int _counter;
public MouseTester()
{
var im = new Manager();
_devId = _im.GetMouseIdFromHandle(MouseHandle);
if (_devId == 0) return;
Console.WriteLine("Hit S to unsubscribe / subscribe");
//var devs = im.GetDeviceList();
//var mouseHandle = @"HID\VID_046D&PID_C539&REV_3904&MI_01&Col01";
var mouseHandle = "HID\\VID_046D&PID_C00C&REV_0620"; // Logitech USB
var devId = im.GetMouseIdFromHandle(mouseHandle);
SetSubscribeState(true);
var counter = 0;
while (true)
{
while (Console.KeyAvailable == false)
Thread.Sleep(250);
var cki = Console.ReadKey(true);
if (cki.Key == ConsoleKey.S)
{
SetSubscribeState(!_subscribed);
}
}
}
if (devId != 0)
private void SetSubscribeState(bool state)
{
if (state && !_subscribed)
{
im.SubscribeMouseButton(devId, 1, true, new Action<int>(value =>
Console.WriteLine("Subscribing...");
_subscribed = true;
_im.SubscribeMouseButton(_devId, 1, true, new Action<int>(value =>
{
Console.WriteLine("RButton Button Value: " + value);
}));
im.SubscribeMouseButton(devId, 3, true, new Action<int>(value =>
_im.SubscribeMouseButton(_devId, 3, true, new Action<int>(value =>
{
Console.WriteLine("XButton1 Button Value: " + value);
}));
im.SubscribeMouseButton(devId, 4, true, new Action<int>(value =>
_im.SubscribeMouseButton(_devId, 4, true, new Action<int>(value =>
{
Console.WriteLine("XButton2 Button Value: " + value);
}));
im.SubscribeMouseButton(devId, 5, true, new Action<int>(value =>
_im.SubscribeMouseButton(_devId, 5, true, new Action<int>(value =>
{
Console.Write("WheelVertical Value: " + value);
var mycounter = counter;
var mycounter = _counter;
mycounter++;
Console.WriteLine(" Counter: " + mycounter);
counter = mycounter;
_counter = mycounter;
}));
im.SubscribeMouseMove(devId, true, new Action<int, int>((x, y) =>
_im.SubscribeMouseMove(_devId, true, new Action<int, int>((x, y) =>
{
Console.WriteLine($"Mouse Move: x: {x}, y: {y}");
}));
}
else if (!state && _subscribed)
{
_subscribed = false;
Console.WriteLine("Unsubscribing...");
_im.UnsubscribeMouseButtons(_devId);
_im.UnsubscribeMouseMove(_devId);
}
}
}
}

@ -7,13 +7,13 @@ namespace TestApp
{
private static void Main()
{
//var mt = new MouseTester();
var mt = new MouseTester();
//var mbt = new MouseButtonsTester();
//var kt = new KeyboardTester();
//var kkt = new KeyboardKeyTester();
//var tt = new TabletTester();
var sct = new ScanCodeTester();
//var sct = new ScanCodeTester();
Console.ReadLine();
}
}

Loading…
Cancel
Save