How to create a hotkey so the user can stop/pause the program.
I created a WPF app that automates mouse movements. When the program is active the user cannot press the stop button since the mouse is being used. How do I create a hotkey that can be pressed and stop the programming.
I searched the internet and I found 2 options, but I don't know which is best
1. Use a side thread for the program and a main thread that listens for any hotkeys. Then the main thread either pauses or kills the side thread.
2. Use Task ? But I'm not sure how that works.
13 Replies
What will it automate?
I have an album of feetpics on my pc but there are faulty photo's as well. It uses AI to check if its actually feet or not and deletes faulty pictures
gotta give credit for a creative non-answer
Usually cancellation is done with
CancellationToken
. Create a CancellationTokenSource
, get a token from it, and when the user hits the hotkey do CancellationTokenSource.Cancel
Inside your working thread have
CancellationTokenSource Class (System.Threading)
Signals to a CancellationToken that it should be canceled.
Why do you need mouse clicks
Does this kill the thread instantly?
Currently I found an alternative option. However, this option doesn't instantly stop the thread. It finishes it's loop first which takes about 5 sec which looks like this:
When the user presses the hotkey the program still runs x seconds. I was hoping for an option that instantly kills the thread and stops the mouse from being interrupted. Was wondering if this was possible without me having to write checks every 2 lines.
cancellation tokens are cooperative, the task/thread has to be periodically checking if it's cancelled or not
you don't want to abort threads abruptly, you can corrupt the state of your program
I thought about that. But there is no data related stuff going on in the workerthread, so I thought I would be fine.
Workerthread is just doing mouse clicks
i'm still confused why you need mouse clicks at all
why does a file processing program need any UI automation
can you not just open the file and feed it into whatever model you're using for categorization?
I could, but I need to automate other stuff as well. So I thought why not just make an autoclicker lol
But it's beside the point anyway
UI automation is basically the most difficult and least reliable way of automating anything
I don't even have to use threads but I want to learn how they work