C
C#9mo ago
! 0Falco

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
Buddy
Buddy9mo ago
What will it automate?
! 0Falco
! 0FalcoOP9mo ago
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
Jimmacle
Jimmacle9mo ago
gotta give credit for a creative non-answer
lycian
lycian9mo ago
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
c#
while (true)
{
cancellationToken.ThrowIfCancellationRequested(); // Or w/e it's called

// do one loop of work here
}
c#
while (true)
{
cancellationToken.ThrowIfCancellationRequested(); // Or w/e it's called

// do one loop of work here
}
Kuinox
Kuinox9mo ago
Why do you need mouse clicks
! 0Falco
! 0FalcoOP9mo ago
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:
while (true)
{
// Do work that takes about 5 sec
worktedThread.Stop()
}
while (true)
{
// Do work that takes about 5 sec
worktedThread.Stop()
}
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.
Jimmacle
Jimmacle9mo ago
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
! 0Falco
! 0FalcoOP9mo ago
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
Jimmacle
Jimmacle9mo ago
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?
! 0Falco
! 0FalcoOP9mo ago
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
Jimmacle
Jimmacle9mo ago
UI automation is basically the most difficult and least reliable way of automating anything
! 0Falco
! 0FalcoOP9mo ago
I don't even have to use threads but I want to learn how they work
Want results from more Discord servers?
Add your server