Call MediaCapture.InitializeAsync from UWP BackgroundTask - c#

I am trying to take a photo during a BackgroundTask at an UWP application.
I read at documentation that InitializeAsync should be on the STA thread but is there some approach to achieve that from a MTA? I don't known, something like Dispatchers or others workarounds.
In my actual scenario, when I call CapturePhotoToStreamAsync just after the InitializeAsync, the following exception occurs:
System.Runtime.InteropServices.COMException (0xC00D3E82): A media
source cannot go from the stopped state to the paused state.
The funny thing is that it works if my app is not suspended.

I don't think you will get it to work in BackgroundTask - as MSDN says:
InitializeAsync should be called from the main UI thread of your app. For more information, see Guidelines for enabling sensitive devices.

Related

UWP network communication in the background

I'm trying to go through the official tutorial for background task network in UWP titled Network communications in the background. However, since the tutorial is poorly ported from C++ code, the code, after being modified to be able to compile, fails at instantiating the ControlChannelTrigger object with an AccessDenied exception.
The exception is thrown at the following line.
channel = new ControlChannelTrigger(channelId, serverKeepAliveInterval,
ControlChannelTriggerResourceType.RequestHardwareSlot);
I read the comment about the lock screen and tried to add the app to the lock screen (even though this should no longer be a limitation), but the error persists.
I'm trying to maintain a persistent WebSocket connection with my own server to allow push notifications (I am aware of WNS, but it is not the right choice for my use-case).
My problem is similar to one in the related question ControlChannelTrigger in UWP Windows 10 access denied.
For simplicity, I am implementing everything as an in-process background task.
Can anyone suggest a solution or a workaround? Could the tutorial be outdated?
Unsurprisingly, I found the answer to my own question again.
The registered background task cannot be an in-process task and must be registered as an out-of-process task.
As for the Windows.Networking.Sockets.WebSocketKeepAlive entry point, I made a workaround by creating a background task that contained the Windows.Networking.Sockets.WebSocketKeepAlive and called its Run(…) method.

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.

Get thread information in Xamarin PCL

I start some parallel tasks from a PCL with
Parallel.Invoke(() => ExecuteTaskAAsync(), () => ExecuteTaskBAsync());
Now I want to know on which thread these tasks are running.
Thread.CurrentThread.ManagedThreadId seems to be only available in the iOS
and Droid project but not in the PCL or UWP project. What are my possibilities? If I would use DependencyService I also won't know on which thread TaskA is running, because there is not relationship between the started task and DependencyService.
How can I get thread information (id, name, ...) in a PCL?
When you set a breakpoint you can get into the Tasks Window and get some more information. Also you have a drop down on the top to select a process, choose between different threads and get some info about the stack frame. So one should be able to also get this information in code?
Edit:
Seems that the informations in the Tasks Window is only available when running on UWP.
The concept of threads is completely absent in Windows Universal Apps and the use of tasks is recommended instead of threads.
I haven't found official documentation on the reasons behind it, but there's an ongoing discussion at the CoreFX's github repo.
So it looks like what you are looking for isn't currently possible if you want to support UWP projects.
You mal alternatively look for the TaskScheduler that manages the currently running task:
var poolId = TaskScheduler.Current.Id;
This way you can at least find out, if your method is running on the same ThreadPool (eg: They are executed on any background threads, or the UI thread).
As far as I know, to find the TaskScheduler for the UI, you have to call TaskScheduler.Current.Id while you are on the UI Thread and just store it in a variable.
This works for me
Environment.CurrentManagedThreadId

Work while app is suspended in Windows Phone 8.1

I want to know what, how, and the limitations of the work that can be done whilst an app is suspended.
I'm not talking about Background Tasks, since those run whether the app is suspended or not, but what you can do while the app is in the background, suspended.
What I noticed so far is that if I have a BackgroundTask with the Completed event attached in the MainApp, that trigger is fired whilst the app is suspended, meaning that I can in fact do work in the MainApp, with the app suspended. So far I only have two ways of doing that, one is by using a BackgroundTask Completed event, and the other is by having a timer that forces something to happen.
However, what, how and the limitations of what I can do with the suspended app are not clear to me.
So, is there another way of working with a suspended app? And how much CPU time do I have, if limited, to work with?
I have played around a bit and found out that MessageWebSocket, if not disposed on app suspension, can still receive messages.
You could use the open socket connection to send messages, based on which you can execute code in the background.
A problem is when your app gets terminated by the OS (when the device does not have enough memory), then the socket connection will be closed too.
For more info on how to implement sockets see here.
You should check this link.
Understanding the App’s Lifecycle and Managing State - By Bob Tabor
Here its shown how you can manage to save the state of the app if your app is being suspended using suspension manager and also how to save data if the app is terminated from the suspension state.
Bob Tabor has clearly explained this in detail.
It was helpful to me, hope this helps you.

background task in windows phone 8.1

I am using a 3rd party rest api to query data and display it in my app. I have to perform a task like at night 12 approx. it will perform a background task to query data from rest api and update live tile and generate notification. I would like to use only C# only for this task. I don't know what will be best approach to do this task. But I using below code to perform background task to do this which is not working. Not sure why?
BackgroundTaskBuilder taskBuilder = new BackgroundTaskBuilder();
taskBuilder.Name = taskName;
SystemTrigger trigger = new SystemTrigger(SystemTriggerType.InternetAvailable, false);
taskBuilder.SetTrigger(trigger);
taskBuilder.AddCondition(new SystemCondition(SystemConditionType.InternetAvailable));
taskBuilder.TaskEntryPoint = typeof(BackgroundTask.BackgroundTask).FullName;
taskBuilder.Register();
and from background task I am querying data and generating toast notification.
Any help why this code is not working or when this task will fire. Is there any better approach to do above task?
Thanks
Regarding the code you have not working...
For Windows Phone 8.1 unlike Windows 8\8.1, you are required to call BackgroundExecutionManager.RequestAccessAsync() (search MSDN\internet) for ANY background task before registering task(s) whereas in Windows this is only required for some tasks. Make sure your code calls this and validate the returned value before registering your background task(s).
Regarding knowing if your task "worked"...
It's a good idea to have the background task implementation run (IBackgroundTask::Run()) independent of the trigger\conditions you've set to ensure it performs without issue by debugging it. See instructions in the following link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/jj542416.aspx.
Regarding your use of SystemConditionType.InternetAvailable...
I'm not 100% about this but I'm pretty certain this will be redundant given you already have a SystemTriggerType.InternetAvailable. I don't know of a situation where the trigger would fire but the condition wouldn't be true.
Regarding the requirement you've mentioned...
If I understand your requirement correctly you have different options here:
If your app is a Windows Phone XAML app that need to run based on time, I would recommend either TimeTrigger or MaintenanceTrigger triggers (as opposed to the SystemTrigger). These are both Background Tasks. For general info on Background Tasks and links to the TimeTrigger and MaintenanceTrigger documentation see this MSDN link: http://msdn.microsoft.com/en-US/library/windows/apps/xaml/hh977056.aspx.
If your app is a Windows Phone Silverlight 8.0 app you can use Background Agents, specifically either PeriodicTask or ResourceIntensiveTask. See the links posted by others or search the MSDN\internet for more info.
If your app is a Windows Phone Silverlight 8.1 app you can use the option in either 1 or 2 above.
I think you should try using PeriodicTask. Also consider the constraints mentioned in the link.
create one class with output Type :Windows Runtime Component
and put your Class that inheritance from IBackroundTask so this work
if you use from emulator for launching app, i think your app for register task not active in emulator.

Categories

Resources