Queue order of background task - c#

I have a windows phone 8.1 app that has several background tasks. They all are called by 15-minute time triggers. So how can I assure that they are executed one after the other?
For example first the background task is called that updates some date, and only after that the background task is called that updates a live tile?

I'm afraid that's currently not possible. Your background tasks will be executed in a certain time frame but there is no guarantee that there's an explicit order.
But if you know that you want to do things in a sequential order why not do everything from one background task? You can always update a live tile manually from a background task (https://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj991805.aspx).

Related

UWP - app in background state and async method

I created app for universal windows platform(for Windows 10 desktop). I use DispatcherTimer in app. Timer run async method. When app in foreground it is work.
But app is background(I minimize a window) async method not work. How can I solve this problem?
When your app is not running in the foreground and tasks need to be executed, it is well known that we need to implement the background tasks for the app.
But, background tasks in UWP are lightweight. Due to memory constraints, battery life issue, I'm not sure what you need can be done in background task of UWP.
That doesn't mean you can't use DispatcherTimer in the background task, but background tasks are limited to 30 seconds of wall-clock usage, and it can be terminated by system for example when it throw the out-of-memory exception. So, if you want to execute your task every one minute, then it will not work.
The TimerTrigger which is mentioned by #ibebbs has a minimal time intervals which is 15 minutes, so I'm also not sure this can be used in your scenario.
Problem is what you need to do in the background task and how often, you can leave a comment to tell that, so can we continue to discuss on this issue.
You should Create and register a background task that runs in a separate process and use a TimerTrigger to invoke it at the desired interval.

Thread vs Backgroundworker vs Timer - Updating UI in C#

I am trying to create a time clock for clocking in and out in C# with windows forms. I would like to have the current time displayed on the screen constantly. I also want to be able to ping the bosses wifi on his or her phone, then use that to determine if they are in the building. I tried using threads, but then they can't update the UI. I also tried using timers, but it was super laggy. I also tried a background worker, but I think that you can't update the UI with a background worker. I would like to be able to update the time every second, and check if the boss is there every minute or so. Please help me find a way to do this without slowing everything down.
You can actually do many things from background call, updating UI is simplest.
Put following code in your background call where UI update is required. UI just needs to invoked from its own thread.
this.Invoke((MethodInvoker)delegate {
LabelStatus.Text = "Yeah its working";
Label2.Text = "Updated UI";
// do your stuff on UI thread
});

Background task cancelled (ExecutionTimeExceeded)

I got a background task that downloads a few JSON data files that works perfectly when I call it using Visual Studio. When the background task is called using the UserPresent trigger at startup it gets cancelled (ExecutionTimeExceeded.)
Is there anything I can do to run the background task without any limit?
Probably not, the time limit is dependent on trigger type, and as MSDN says it can vary from 30 seconds to 10 minutes:
CPU quotas: Background tasks are limited by the amount of wall-clock usage time they get based on trigger type. Most triggers are limited to 30 seconds of wall-clock usage, while some have the ability to run up to 10 minutes in order to complete intensive tasks. Background tasks should be lightweight to save battery life and provide a better user experience for foreground apps. See Support your app with background tasks for the resource constraints applied to background tasks.
The backgroundtask should be a lightweight code - maybe you can redirect file download procedure to background transfer service and do some of heavy job in main app. You may also try with MaintenanceTrigger, there is a chance that constraints are lower for this one, but it fires once phone is connected to AC.

How to run background process in C# WPF

I am completely new to WPF.
I am writing a simple ui for patient monitoring. In the main screen, I have 3 beds i.e. 3 Buttons
I want to run some background process which basically change the background color of each of the button based on some condition. Each button will have its own process before it updates the color.
Multithreading is a large beast to tackle and even more difficult to debug if you don't fully understand the characteristics of multi threaded environments. Be careful of deadlocks, inadvertently overwriting data in a different thread, and race conditions. With this said I would suggests 2 things to first explore when writing multithreaded applications.
background workers and async functions.
I would start with the background worker and try to send it generics and such, but keep in mind that any changes to the GUI must be done on the same thread that created the GUI (main thread). So callbacks must be in place which the on completed event of the background worker will allow you to do.

How to use Time Trigger in background task in Windows 8 JS Metro App

I have a requirement where I have to call a service in background after every let say 1 hour to get some informations from server. I am working on JavaScript Metro Application. I have tried the background task and used Time Trigger and I have scheduled it to get triggered in every 15 minutes. It get called first time and then it is never called. I didn't close the background task because I want it to run all the time and call the service at scheduled time.
I have used the Microsoft Background task sample for reference.
Please tell me what should be the best approach to call a service in background.
How to use Time Trigger and Why Time Trigger doesn't get called after first time?
Please share code sample or walkthrough if any.
Thanks
First thing you should do is to close the background task properly as instructed in the documentation - if your tasks don't behave nicely, platform might suspend and refuse to run them for some time. You should let the platform handle triggering of the events based on the triggers and conditions you define instead of trying to bend the system. Also, remember that there's CPU and data usage quotas for background tasks present, one can't do massive amount of processing in background tasks - if the quotas are exceeded, tasks will get suspended. Be also sure that the background task works and doesn't throw errors.
In general, my recommendation is that one shouldn't rely solely on background tasks to fetch the information since it's not guaranteed that they manage to do it on time, so better to prepare for downloading the needed data in the foreground app as well. This obviously depends on the use case: if the data fetched in background tasks is not critical but more like nice-to-have, there's much less to worry about.
The TimeTrigger requires the app being added to the lock screen (see docs), but I guess you already meet this requirement since you've managed to get the task running once.
For debugging the background tasks, please take a look at Event Viewer, see detailed instructions. That page also contains some tips about common problems. The Event Viewer entry mentioned in that document is often a valuable resource for figuring out problems with the bg task execution. My guess is that you'll see errors there related to not closing the task properly.

Categories

Resources