How to run a scheduled task using electron.net - c#

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.

Related

Long-running task without IHostedService running the entire life of the application?

I have a website page that needs the option of performing an operation that could take several minutes. To avoid performance issues and time outs, I want to run this operation outside of the HTTP request.
After some research, I found IHostedService and BackgroundService, which can be registered as a singleton using AddHostedService<T>().
But my concern is that a hosted service is always running. Doesn't that seem like a waste of resources when I just want it to run on demand?
Does anyone know a better option to run a lengthy task, or a way to use IHostedService that doesn't need to run endlessly?
Note that the operation calls and waits for an API call. And so I cannot report the progress of the operation, nor can I set a flag in a common database regarding whether the operation has completed.
One option to run a lengthy task on demand while avoiding performance issues and time outs is to use a message queue. You can have your Razor Pages website send a message to the queue when the operation is requested, and have a separate service, such as a background worker, consume messages from the queue and perform the operation. This allows you to decouple the task from the web request, and also allows for the possibility of adding more worker instances to handle the workload.
Another option is to use a task scheduler that runs on demand, such as Hangfire. It allows you to schedule background jobs and monitor their progress, which can be useful in your scenario where you cannot report the progress of the operation.
You can also use IHostedService, but you need to make sure that the service is only running when it is needed. You can use a flag or a semaphore to control whether the service is running or not. You can set the flag or semaphore when the operation is requested, and clear it when the operation is completed. The service can then check the flag or semaphore in its main loop, and exit if the flag is not set.
In summary:
message queue, task scheduler, and IHostedService with controlling flag/semaphore are all viable options for running a lengthy task on demand. The best option depends on your specific use case and requirements.

Wait for Tasks to complete at shutdown without deadlocking

At any given time, my WPF application has a bunch of async Tasks running on background threads/synchronization contexts. At shutdown, I need to set a CancellationToken and then wait for these tasks to complete gracefully before I can start tearing down native libraries they depend on. But some of the tasks have continuations that (need to) run on the main thread, so calling Task.WaitAll(_myTasks) from the same thread will deadlock. What I'd like to do is something like this:
while(!AreAllTasksComplete(_myTasks))
LetContinuationsThatAreWaitingForThisContextRun();
TearDownTheNativeLibraries();
But of course LetContinuationsThatAreWaitingForThisContextRun() does not exist. Is there any way to do what I'm trying to do? Do I need a completely different approach?
You can do
await Task.WhenAll(_myTasks);
TearDownTheNativeLibraries();

Monitoring a multi-threaded application

I am writing a multi-thread application (using C#) where the job of each thread is to insert some data into database. As soon the thread completes its job of inserting data into database it becomes free (i.e. ready to insert another data into database). All the threads are reading data from a queue.
The problem is, how to monitor which thread has completed its current job and ready to take second job? Whether we can use C# task instead of thread and how?
Please note every thread is inserting data to the same database.
The problem is, how to monitor which thread has completed its current job and
ready to take second job?
Why would you do that? Threads created are looping until there is no data. If there is no thread (or less than wanted) and data arrives, start a new thread/task. Actually uses tasks. There is no need to monitor them. This would be ridiculously inefficient.
Whether we can use C# task instead of thread and how?
Yes, and it is as simple as "look up how to start a task, which google has an answer for". That said, your architecture likely needs adjustment - doing too many things in parallel will only waste memory, rather limit the number of active threads/tasks to a specific number.
In my opinion you should use only Task not "Task in Thread".
Tasks are more flexible and already implemented robustly. In your case you can create an Task[] (with 10 Tasks if you want) and to know if the task has completed his work you can check the Task.Result value if you have declared Task<\TResult> objects.
in this way you can have direct control of the processes during the asynchronous execution including exception handling.

Break the running function or procedure manually

I've met such a problem: my function takes a very long time to run. If a user runs that function by mistake he must wait until the function gets to the end.
Is it possible to stop a function by clicking "Escape", for example? I just can't send anything to the function while it is running.
Thanks, Andrew.
The wrong approach would be to run your method on a separate thread, and abort that thread when it is needed.
The right approach is to pass some marker to the method and method itself would check it from time to time, returning if the marker says that it should. You can use TPL with CancellationToken to do just that.
You can run the task on a different thread, and if the user wants to finish the task, then you might call yourThread.Stop() or yourThread.Abort()
Anyway, you should control your process before just killing it, that way it is safer.
Hope that helps!
You should run your function in parallel, using a thread, a BackgroundWorker or the Task parallel library.
This will allow you to keep the user interface responsive and to cancel the background operation.

How do I cancel a schedule for a task?

I need to create a scheduled task. For that I'm trying to learn how to do that, and am currently thinking of using TaskSchedulerClass from TaskScheduler Class Library. I got a code examples here (and there's another one I might try here). But - I don't want to actually run it without making sure I know how to cancel it when needed.
So my question is: How do I cancel such a task-registration in code?
(Just "Try it!" won't suffice in this case. Because the point is I don't want to get stuck with something I can't stop.)
What about TryDequeue Method?
It's attempts to dequeue a Task that was previously queued to this scheduler.
You need to use the same Task you sent to QueueTask function.
If you don't have the instance of this Task you can try and retrieve it using GetScheduledTasks, but it stated it in use For debugger support only

Categories