Background Task in xamarin forms [duplicate] - c#

I want yo make a task which complete running after Quit the app. Like complete Downloading Task. I don't find active way or method for Xamarin form mobile app. Any Help?

All You Want to Do is You Want your Task Run in Background so Even if you Quit the App the Task continues to Run is it okie ?
This Type of Task Are Known As Services(Back ground Tasks,or Background Services) in .net And Xamarin PlatForms
I Think You will Find This link Helpfull
See this : enter link description here
in this Way if you are running Some Downoading Task then it will not be killed After App is Getting quit or onSleep and will keep running in Back Ground Until Its Completed Or Any Error Generate ..

First, if you want to make the app still be running after quitting, It can not be realized.
Second, if you want to make the app quit after finishing the task you can refer to the How to terminate a Xamarin application?

Related

How to perform a simple background task on Xamarin iOS

In our app users can track and submit journeys they have recorded. I need a simple way of creating a task in iOS. I have already created and tested this on Android. It works via:
The user selects the journeys they would like to submit.
Taps sync and a foreground service is created that syncs the journeys to our API.
This service will continue to sync journeys even if the app is put into the background or even closed.
So in short how can i achieve this on iOS 9-13?
I have already tried creating a background-safe tasks using:
nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
However, this only gives the task 2 mins to run which isn't enough. I have looked into the NSURlSessions but they require URls, whereas we are using our own API wrapper.
I would simply like a way of creating a task when the user taps 'sync' and this task also being capable to run in the background. I am not too bothered if the task is cancelled when the app is closed, although if possible would like the task to continue.
Thanks.
This service will continue to sync journeys even if the app is put
into the background or even closed.
First, if your app is closed in iOS, I'm sure do can't run any service in background.
Then if your app is put into background, Apple has strict limit to allow apps running in the background. background-tasks has time limits, you can read the document about more information. There is a section about handling-background-task-time-limits which you can have a try.
Also, Apple allows some specific apps to run in background which have to perform tasks in the background. For example, app that needs to play music in background, update location in background and etc. You can see the Application Registration Categories here. If your app meets the requirement there, you can apply for a background running permission from system.
Refer: Backgrounding in Xamarin.iOS
I would advise you to leverage on Shiny to achieve it.
PerformFetch is the closest thing to what you ask for, it will run in the background and update your app when iOS thinks it is needed (it predicts that according to the previous behavior the user will soon open your app and that the new content is available).
The only alternative is to send the push notification when you want the app to be updated.
That's about it, I understand your wish but it is just that - a wish and not something that can be real.

UWP Start background task at startup

I just started learning UWP and i'm really confused on how it works. I already saw tens of posts that talk about my problem but can't figure out how to do what I want.
So I want to make an app that runs on windows startup, I want the app to be not visible so it needs a background Task, how can I trigger this background task without getting to the app UI ?
The app is supposed to have the Background Task always running, and its interface is supposed to be used as "settings" so I don't need the app to be shown on startup.
Thanks.
I found my way here after a lot of googling. to be honest I have come to the same conclusion as Motaz. But as of writing this I am way too invested in what I have already. While what I have here is not the perfect answer to his question. I wanted to come back and post what I've learned for anyone else who ends up here.
My need is a app that when started will monitor a third party USB device until the app is closed (regardless of whether the app is minimized or not)
Windows Template Studio is good, but the docs not so much. Especially when it comes to Background tasks.
I started here: https://github.com/Microsoft/Windows-universal-samples. But there is two problems.
They combined all the examples and some of the code is shared, which makes it difficult to pull a project out and hack it apart without breaking the original examples.
Following their background task example I perpetually had issue with the manifest and and it wanting an audio task
I went back to the template studio and created the simplest version with a background task possible. After a lot of trial and error I got something that works. I have posted it here: https://github.com/PillarOfSociety/WindowsTemplateStudio-BackgroundTask
Some things to note:
I am no expert on UWP and while this runs I have no intent on putting it in the store nor did I try.
If you do download my project and run it, you should be able to just hit the "Add events" and the "Start Back" button and the task should run and update the Progress bar.
I used an ApplicationTrigger. the original example from the template uses a TimeTrigger which takes time in MINUTES (took me too long to figure that out). Supposedly Application triggers have a 30sec timeout.. but on my laptop they live for much longer.. I don't know how long. This page is useful: https://learn.microsoft.com/en-us/windows/uwp/launch-resume/support-your-app-with-background-tasks
The template studio generated BackgroundTaskService will leave background tasks registered after the app is closed, but will NOT make the connection back to them once its rerun, so on a rerun either the task appears not to run, or will crash the app when triggered.
Important Code I discovered:
foreach (var task in BackgroundTaskRegistration.AllTasks)
{
TestText += task.Value.Name; //gets the typename of the task
//task.Value.Unregister(true); //will unregister tasks
}
await Task.CompletedTask;
The tasks in BackgroundTaskRegistration.AllTasks are not the BackgroundTask class that the template studio uses. IN my example I unregister all of them each time it runs so that you have a reference to the task as an instance of BackgroundTask.
Hopefully this helps someone and Good luck!
If you're just starting out use Windows Template Studio, it will be perfect for you. It is an extension of Visual Studio which lets you create new uwp projects with a lot of built in features, and you can only choose the features you want. It will save you a lot of time on basic stuff.
https://github.com/Microsoft/WindowsTemplateStudio

Continue an operation even the app get into suspended or terminated state

I'm new to the UWP, and I'm facing right now an issue where I want to continue an operation that the app was doing before getting into suspended or terminated state.
I've read about Extended execution and background task, but as far as I have understood for extended execution you have only 30 seconds before it gets terminated or it could be revoked before getting into it.
for Background task I should make another project for it ( Windows Runtime Component) and I have to add an entry in the Declarations in the appxmanifest. It sounds that Background task is the only possible way to achieve it, but how to move an operation (Action, Func, or task or whatever) to background task if the app get into those states and what should happen after resume?!
Any ideas from experienced people?
Windows 10 universal Windows platform (UWP) app lifecycle
Before Windows 8, apps had a simple lifecycle. Win32 and .NET apps are either running or not running. Now, there are three app model in UWP app Running in foreground,Running in background and suspended state. You could know more detail through this official document.
Extended execution
There are cases where an app may need to keep running, rather than be suspended, while it is minimized. If an app needs to keep running, either the OS can keep it running, or it can request to keep running.
For this scenario, you need use ExtendedExecution to realize. ExtendedExecution support to start a long running operation in order to defer the Suspending state. And there are some document and code sample introducing this feature.
Background Task
For Background Task, it provide functionality when your app is suspended or not running. You can also use background tasks for real-time communication apps like VOIP, mail, and IM. However, it will trigger under specific conditions. For more please refer this.
You have mentioned out-process Background Task in your case and another Background Task(in process) could also be used in UWP app. It is simpler to implement than out-of-process background tasks.

How to programmatically close the print UI on a Windows 10 application?

I am designing a Windows 10 Universal application using this https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing/cs repository as a guide. I currently have fully functional printing by calling:
await Printmanager.ShowPrintUIAsync();
In my application there is also an activity timer that logs the user out after a certain amount of time inactive. This part is working fine, but I am unable to close the print ui upon logout.
Note: Normally to close a windows async operations, you can do something similar to:
IAsyncOperation<bool> printOperation = Printmanager.ShowPrintUIAsync();
printOperation.Cancel();
This works for other AsyncOperation occurences, but I cannot get it to work for the print UI, as the print UI is not a child process of the app, but is a seperate process itself
Thanks in advance!
Also, it seems there was a solution to kill processes in Windows 8 which is no longer supported in Windows 10 applications (Process.GetProcessByName .... or FindWindow)
Perhaps there is someway to kill a Windows 10 process by name?
You can't do that.
All the Metro-style applications work in the highly sandboxed environment and there is no way to directly start an external application.
Taken from this article
maybe the solution is creating a proxy.
How to Start a external Program from Metro App
This could help you, because you can kill the process indirectly.
I hope this helps.

Background task visible in lifecycle events during debug but doesnt run automatically

Issue
I want to use a background task to show toast notification in windows phone 8.1. I followed this article to add the background task Microsoft article. The problem is that the background task never runs. Its runs only when I call it from lifecycle events when In debug mode.
this is very tricky. but i assume there might be a lot of other background tasks running at the sametime which are not allowing your task to run.
try restarting the phone and then launch this app first. that should solve it.

Categories

Resources