Exemplo n.º 1
0
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            if (taskInstance == null)
            {
                Diag.DebugPrint("KATask: taskInstance was null");
                return;
            }

            Diag.DebugPrint("KATask " + taskInstance.Task.Name + " Starting...");

            // Use the ControlChannelTriggerEventDetails object to derive the context for this background task.
            // The context happens to be the channelId that apps can use to differentiate between
            // various instances of the channel.
            var channelEventArgs = (IControlChannelTriggerEventDetails)taskInstance.TriggerDetails;

            ControlChannelTrigger channel = channelEventArgs.ControlChannelTrigger;

            if (channel == null)
            {
                Diag.DebugPrint("Channel object may have been deleted.");
                return;
            }

            string channelId = channel.ControlChannelTriggerId;

            if (((IDictionary <string, object>)CoreApplication.Properties).ContainsKey(channelId))
            {
                try
                {
                    AppContext appContext = null;
                    lock (CoreApplication.Properties)
                    {
                        appContext = ((IDictionary <string, object>)CoreApplication.Properties)[channelId] as AppContext;
                    }
                    if (appContext != null && appContext.CommunicationInstance != null)
                    {
                        CommunicationModule communicationModule = appContext.CommunicationInstance;
                        Task <bool>         result;
                        lock (communicationModule)
                        {
                            result = communicationModule.SendKAMessage(GetType().Name);
                        }
                        if (result != null && result.Result == true)
                        {
                            // Call FlushTransport on the channel object to ensure
                            // the packet is out of the process and on the wire before
                            // exiting the keepalive task.
                            communicationModule.channel.FlushTransport();
                        }
                        else
                        {
                            // Socket has not been set up. reconnect the transport and plug in to the ControlChannelTrigger object.
                            communicationModule.Reset();

                            // Create CCT enabled transport.
                            communicationModule.SetUpTransport(communicationModule.serverUri, GetType().Name);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Diag.DebugPrint("KA Task failed with: " + ex.ToString());
                }
            }
            else
            {
                Diag.DebugPrint("Cannot find AppContext key channelOne");
            }

            Diag.DebugPrint("KATask " + taskInstance.Task.Name + " finished.");
        }