public void Run(IBackgroundTaskInstance taskInstance) { if (taskInstance == null) { Diag.DebugPrint("NetworkChangeTask: taskInstance was null"); return; } NetworkStateChangeEventDetails details = taskInstance.TriggerDetails as NetworkStateChangeEventDetails; // Only restart CCT connection if network connectivity level changes. if (details.HasNewNetworkConnectivityLevel == false) { return; } Diag.DebugPrint("System task - " + taskInstance.Task.Name + " starting ..."); // In this example, the channel name has been hardcoded to lookup the property bag // for any previous contexts. The channel name may be used in more sophisticated ways // in case an app has multiple ControlChannelTrigger objects. string channelId = "channelOne"; 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 communicationInstance = appContext.CommunicationInstance; // Clear any existing channels, sockets etc. communicationInstance.Reset(); // Create CCT enabled transport. communicationInstance.SetUpTransport(communicationInstance.serverUri, GetType().Name); } } catch (Exception ex) { Diag.DebugPrint("Registering with CCT failed with: " + ex.ToString()); } } else { Diag.DebugPrint("Cannot find AppContext key channelOne"); } Diag.DebugPrint("System task - " + taskInstance.Task.Name + " finished."); }
async private void ConnectButton_Click(object sender, RoutedEventArgs e) { if (connectionState == ConnectionStates.NotConnected) { if (!Uri.TryCreate(ServerUri.Text.Trim(), UriKind.Absolute, out serverUri)) { Diag.DebugPrint("Please provide a valid URI input."); return; } ConnectButton.Content = "Connecting..."; connectionState = ConnectionStates.Connecting; // Finally, initiate the connection and set up transport // to be CCT capable. But do this heavy lifting outside of the UI thread. bool result = await Task <bool> .Factory.StartNew(() => { return(communicationModule.SetUpTransport(serverUri, GetType().Name)); }); Diag.DebugPrint("CommunicationModule setup result: " + result); if (result == true) { ConnectButton.Content = "Disconnect"; connectionState = ConnectionStates.Connected; } else { ConnectButton.Content = "Failed to connect. Click to retry."; connectionState = ConnectionStates.NotConnected; } } else if (connectionState == ConnectionStates.Connected) { await Task.Factory.StartNew(() => { communicationModule.Reset(); }); connectionState = ConnectionStates.NotConnected; ConnectButton.Content = "Connect"; } }
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."); }