Monitoring Edge.js Tasks from C# - c#

My goal is to be able to cancel a long running Node.js task that has been called from C# using Edge.js.
When using tasks normally, you can specify a cancellation token, give your task some time to execute, then cancel it if it is going beyond your time limit.
I can't figure out how to define/call the Edge.Func Node function/task and pass in a cancellation token to be able to kill the process if it goes too long. Maybe I'm thinking about this from the wrong angle...
Any insight would be most helpful, thanks!

Related

Cancel python function from dotnet (Python.NET)

I am using Python.NET to call the EasyOCR Python library. Depending on the context, the detection can be slow (from 30s to a couple of minutes) and some actions performed by my user could result in the cancellation of the task. As far as I can tell, the cancellation token will cancel the task only at the next C# instruction so I am stuck while the python code is running.
I have seen that there is some similar mechanism on Python side using asyncio but I was wondering if there was any way to avoid the complexity of having extra python logic by being able to somehow "kill" the python process. Or maybe some easy way to somehow share the token.
A feature very similar to what you are asking has been added to Python.NET recently (you might need to use 3.0 previews though, it probably did not make it into 2.5).
This is the test code, that was added for the feature. I think that is the best reference you will be able to get: https://github.com/pythonnet/pythonnet/pull/1337/files#diff-e846713ba20ecf06af2cc88cc1e92bae49d519998b093d2fb0f7fd6644b10092
Pay attention to the SetUp method too. It shows how to correctly set up multithreading.
There is no good way to abort a non-cooperating thread/task. There is Thread.Abort, but using it is a bad idea. Your options are more or less
Figure out a way to forward the cancellation request to the python code.
Stop waiting for the result when cancelling the task, but let the task continue to run in the background.
Run the task in a separate process, and kill the process on cancel.
Do not allow the task to be cancelled.

Long running async task and long running time limited async task in C#

I have two situations that I'm not sure how to resolve properly. First is a task that runs for as long as the application. Currently, I have a construction like this:
this.task = this.joinableTaskFactory.RunAsync(async () => await this.TaskProcedureAsync().ConfigureAwait(false), JoinableTaskCreationOptions.LongRunning);
Here TaskProcedureAsync is usually a loop until the application end signal is received and inside of it is await of some sort that provides a job to complete (e.g. from an async queue), when it arrives, it is processed and then it waits again for the next job.
Here I'm not sure if running with JoinableTaskCreationOptions.LongRunning is a correct solution for such a task. In DisposeCoreAsync then I await this joinable task:
await this.task.JoinAsync().ConfigureAwait(false);
and then do the rest of the disposal work. So the question here, if this is right thing to do in such scenario or if there is a better way to run such long running async task. This seems to work fine as it is, just not sure if it is optimal regarding the internal management of threads. There are not that many of these loops in the application, up to 30 is a reasonable expectation for my application.
The follow up problem I am quite sure that does not work well as I have it now. Besides those loop tasks that I have that run for as long as the application, I also have a bigger number of long running task that are supposed to be strictly time limited. Currently I use TplExtensions.WithTimeout extension:
result = await script.InitializeAsync().WithTimeout(TimeSpan.FromSeconds(ScriptInitializationTimeoutSeconds)).ConfigureAwait(false);
Basically I am running an untrusted code that must be stopped after certain limit. Now the problem is that WithTimeout does not stop the task itself, only throws exception after the time is out. The question here is how to implement this properly? I was thinking that if the code is to be stopped, it should be run in its own thread as a thread can be killed while task can not. But then I've read here that such an approach would not work because I could only execute synchronous code in such a thread and not async code.
Is there any way to kill the task then? Can I for example kill the thread that currently executes the task? Or how is this done properly?

Simulate preemptive scheduling mechanisms in C#

I have reserched and I realised that actually there is no way to make preemptive scheduling mechanisms in C#, am I right?
So, I have to simulate it by using cooperative scheduling mechanisms.
Actually I have to make API, so my idea is to use some Token, and if that Token is set, then I have to stop currently running Task, run Task with bigger priority, and then continue stopped Task..
So, here is my question. I can not find a good way to stop Task, and continue it later, so is there any way to do that. I have one idea to do nothing in while loop until that token is set, so it will seem like I stopped that Task, and continue later (this is just simulation).. But I hope that there is some better way..
Also I have find EventWaitHandle Class, but I am not sure if I can manage with individual Threads or Tasks.
Actually, I have to make my own Scheduler (of course not something really good, just simulation).. So I have to give him Tasks with priorities, and Scheduler have to schedule that, but if in some moment I add one more Task, and if it has bigger priority, I have to stop current Task, run Task with bigger priority, and when this Task finish, I have to continue stopped Task.. So I have to know if there any good way to suspend Task (but without using Suspend function, because it can make deadlock, and not good to use)...

How to run a scheduled task using electron.net

I need a task to run every 60 seconds to go out and fetch data from a web api. Once the data comes back it will process the data which should not freeze the gui when processing. Is there a way to do this using electron.net?
Yes, in order to do that you must make this task in another thread, one way to do that is to use Microsoft BackgroundWorker (https://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker(v=vs.110).aspx).
Another potential here would be to use a Task, then provide a CancellationToken to it so that during program shut-down, you can dispose of the Task gracefully.

Scheduled Task Time Constraint

I've read through a few posts on SO on whether to use a Windows Service or Scheduled Task and from my understanding I should be using a Scheduled Task.
I have a simple program, basically do a little logic and send an email. The only hard requirement that I have is the email must be sent on the :40 minute mark of each hour. So 8:40, 9:40, 10:40, etc. When I initially setup the schedule for the task I can set it to start at 8:40, recur every hour, every day.
That seems to fulfill the requirement but should I be worried about anything in regards to ensuring the task is ran on that schedule?
It all seems so simple that I'm sure I'm missing something?
Well few points to mention
If you are using a batch file make sure other programs can replace or change it.
Use and access log to log whether the execution was successful or not so you can monitor later(Debugging you application)
Use schedule task errors to check whether your application ran correctly (http://support.microsoft.com/kb/308558)
Use as few privileges as possible.(which is the default)

Categories