Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to terminate ActionTriggers right way
#1
Hallo,
I'm plan to use LibreAutomate-Nuget-Package inside a windows service to respond on third party windows etc. with configured ActionTriggers.
After creating the trigger functions, I'll call ActionTriggers.RunThread() infinitly. If the service itself should be terminating I currently see no option to inject a CancellationToken as it would be normally. Additionally the host machine use hybernating states and wakeup or resume later but normally never will be shutdown fully - so killing threads or creating zombies is not really a good option (also I look to prevent memory leaks).

I would therefore suggest to extend the library with following member function:
async Task ActionTrigger.ExecuteAsync(CancellationToken stoppingToken)

If there is already a way to safely cancel ActionTrigger Tasks by sending messages to thread or other solutions, I would love to know more about it.

Thanks in advance for any advice.
#2
`ActionTriggers.Stop`.
 
Code:
Copy      Help
// script "ActionTriggers cancellation.cs"
using Au.Triggers;

ActionTriggers Triggers = new();

//this thread will stop triggers after 5 s
_ = Task.Run(() => {
    5.s();
    Triggers.Stop();
});


Triggers.Hotkey["Ctrl+K"] = o => { print.it(o); };

Triggers.RunThread();

print.it("stopped");

Quote:inside a windows service to respond on third party windows etc
If it's a true Windows service, triggers won't work in its process, because services run in non-interactive session 0.
#3
Thank you Gintaras for your suggestion to use Stop(). I'm going to implement not a pure windows service but as service-program starting from Taskmanager at logon-event of user to have access to user session and corresponding windows messages.

So I'll try following code:

    var triggers = new ActionTriggers();

    var threadTrigger = new Task(() =>
    {
      // init and configure triggers

      triggers.Run();
    });
    threadTrigger.Start();

    var cancelToken = new CancellationToken();
    var threadStopper = new Task(() =>
    {
      while (!cancelToken.IsCancellationRequested) ;
      {
        // read external msg to receive CancelRequest

        if (cancelToken.IsCancellationRequested)
        {
          triggers.Stop();
        }
        Thread.Sleep(5000);
      } 
    });
    threadStopper.Start();

    Task.WhenAll([threadTrigger, threadStopper]).Wait();


Forum Jump:


Users browsing this thread: 1 Guest(s)