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 = taskInstance.TriggerDetails as IControlChannelTriggerEventDetails; 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 { var appContext = ((IDictionary <string, object>)CoreApplication.Properties)[channelId] as AppContext; string KeepAliveMessage = "KeepAlive Message"; if (appContext != null && appContext.CommInstance != null) { CommModule commModule = appContext.CommInstance; bool result; result = commModule.SendKAMessage(KeepAliveMessage); if (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. commModule.channel.FlushTransport(); } else { // Socket has not been set up. reconnect the transport and plug in to the controlchanneltrigger object. commModule.Reset(); // Create RTC enabled transport. commModule.SetupTransport(commModule.serverName, commModule.serverPort); } } } catch (Exception exp) { Diag.DebugPrint("KA Task failed with: " + exp.Message); } } else { Diag.DebugPrint("Cannot find AppContext key channelOne"); } Diag.DebugPrint("KATask " + taskInstance.Task.Name + " finished."); }