Пример #1
0
        private async void ExtendedExecutionSessionRevoked(object sender, ExtendedExecutionRevokedEventArgs args)
        {
            //If session is revoked, make the OnSuspending event handler stop or the application will be terminated
            if (cancellationTokenSource != null)
            {
                cancellationTokenSource.Cancel();
            }

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                switch (args.Reason)
                {
                case ExtendedExecutionRevokedReason.Resumed:
                    // A resumed app has returned to the foreground
                    rootPage.NotifyUser("Extended execution revoked due to returning to foreground.", NotifyType.StatusMessage);
                    break;

                case ExtendedExecutionRevokedReason.SystemPolicy:
                    //An app can be in the foreground or background when a revocation due to system policy occurs
                    MainPage.DisplayToast("Extended execution revoked due to system policy.");
                    rootPage.NotifyUser("Extended execution revoked due to system policy.", NotifyType.StatusMessage);
                    break;
                }

                suspendDeferral?.Complete();
                suspendDeferral = null;
            });
        }
Пример #2
0
        async private static void OnDeferredImageRequestedHandler(DataProviderRequest request, StorageFile imageFile)
        {
            // Since this method is using "await" prior to setting the data in DataPackage,
            // deferral object must be used
            var deferral = request.GetDeferral();

            // Use try/finally to ensure that we always complete the deferral.
            try
            {
                using (var imageStream = await imageFile.OpenAsync(FileAccessMode.Read))
                {
                    // Decode the image
                    var imageDecoder = await BitmapDecoder.CreateAsync(imageStream);

                    // Re-encode the image at 50% width and height
                    var inMemoryStream = new InMemoryRandomAccessStream();
                    var imageEncoder   = await BitmapEncoder.CreateForTranscodingAsync(inMemoryStream, imageDecoder);

                    imageEncoder.BitmapTransform.ScaledWidth  = (uint)(imageDecoder.OrientedPixelWidth * 0.5);
                    imageEncoder.BitmapTransform.ScaledHeight = (uint)(imageDecoder.OrientedPixelHeight * 0.5);
                    await imageEncoder.FlushAsync();

                    request.SetData(RandomAccessStreamReference.CreateFromStream(inMemoryStream));
                }
            }
            finally
            {
                deferral.Complete();
            }

            MainPage.DisplayToast("Image has been set via deferral.");
        }
Пример #3
0
        private void OnTimer(object state)
        {
            var startTime   = (DateTime)state;
            var runningTime = Math.Round((DateTime.Now - startTime).TotalSeconds, 0);

            MainPage.DisplayToast($"Extended execution has been active for {runningTime} seconds");
        }
        private async void OnTimer(object state)
        {
            var    geolocator = (Geolocator)state;
            string message;

            if (geolocator == null)
            {
                message = "No geolocator";
            }
            else
            {
                Geoposition geoposition = await geolocator.GetGeopositionAsync();

                if (geoposition == null)
                {
                    message = "Cannot get current location";
                }
                else
                {
                    BasicGeoposition basicPosition = geoposition.Coordinate.Point.Position;
                    message = $"Longitude = {basicPosition.Longitude}, Latitude = {basicPosition.Latitude}";
                }
            }
            MainPage.DisplayToast(message);
        }
 private void OnTaskCompleted()
 {
     taskCount--;
     if (taskCount == 0 && session != null)
     {
         EndExtendedExecution();
         MainPage.DisplayToast("All Tasks Completed, ending Extended Execution.");
     }
 }
Пример #6
0
        private async void OnSuspending(object sender, SuspendingEventArgs args)
        {
            suspendDeferral = args.SuspendingOperation.GetDeferral();

            rootPage.NotifyUser("", NotifyType.StatusMessage);

            using (var session = new ExtendedExecutionSession())
            {
                session.Reason      = ExtendedExecutionReason.SavingData;
                session.Description = "Pretending to save data to slow storage.";
                session.Revoked    += ExtendedExecutionSessionRevoked;

                ExtendedExecutionResult result = await session.RequestExtensionAsync();

                switch (result)
                {
                case ExtendedExecutionResult.Allowed:
                    // We can perform a longer save operation (e.g., upload to the cloud).
                    try
                    {
                        MainPage.DisplayToast("Performing a long save operation.");
                        cancellationTokenSource = new CancellationTokenSource();
                        await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token);

                        MainPage.DisplayToast("Still saving.");
                        await Task.Delay(TimeSpan.FromSeconds(10), cancellationTokenSource.Token);

                        MainPage.DisplayToast("Long save complete.");
                    }
                    catch (TaskCanceledException) { }
                    break;

                default:
                case ExtendedExecutionResult.Denied:
                    // We must perform a fast save operation.
                    MainPage.DisplayToast("Performing a fast save operation.");
                    await Task.Delay(TimeSpan.FromSeconds(1));

                    MainPage.DisplayToast("Fast save complete.");
                    break;
                }

                session.Revoked -= ExtendedExecutionSessionRevoked;
            }

            suspendDeferral?.Complete();
            suspendDeferral = null;
        }
        private async void RaiseToastAfterDelay(int id, int seconds)
        {
            using (var deferral = GetExecutionDeferral())
            {
                try
                {
                    await Task.Delay(seconds * 1000, cancellationTokenSource.Token);

                    MainPage.DisplayToast($"Task {id} completed after {seconds} seconds");
                }
                catch (TaskCanceledException)
                {
                    MainPage.DisplayToast($"Task {id} canceled");
                }
            }
        }