Ejemplo n.º 1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            SignalFileLoggerProvider.ForceAddBGLog(LibUtils.GetBGStartMessage());
            SignalLogging.SetupLogging(false);
            Deferral               = taskInstance.GetDeferral();
            ToastNotifier          = ToastNotificationManager.CreateToastNotifier();
            taskInstance.Canceled += OnCanceled;
            bool locked = LibUtils.Lock(5000);

            Logger.LogTrace("Locking global finished, locked = {0}", locked);
            if (!locked)
            {
                Logger.LogWarning("App is running, background task shutting down");
                Deferral.Complete();
                return;
            }
            GlobalResetEvent = LibUtils.OpenResetEventUnset();
            Task.Run(() =>
            {
                GlobalResetEvent.WaitOne();
                Logger.LogInformation("Background task received app startup signal");
                ResetEvent.Set();
            });
            try
            {
                Handle = SignalHelper.CreateSignalLibHandle(true);
                Handle.SignalMessageEvent += Handle_SignalMessageEvent;
                Handle.BackgroundAcquire();
                ResetEvent.WaitOne();
            }
            catch (Exception e)
            {
                Logger.LogError("Background task failed: {0}\n{1}", e.Message, e.StackTrace);
            }
            finally
            {
                Logger.LogInformation("Background task shutting down");
                Handle.BackgroundRelease();
                LibUtils.Unlock();
                Deferral.Complete();
            }
        }