Esempio n. 1
0
        public async void Run(IBackgroundTaskInstance taskInstance)
        {
            NetworkStateChangeEventDetails details = taskInstance.TriggerDetails as NetworkStateChangeEventDetails;

            if (!details.HasNewNetworkConnectivityLevel)
            {
                return;
            }

            var connectionProfile = NetworkInformation.GetInternetConnectionProfile();

            if (connectionProfile.GetNetworkConnectivityLevel() != NetworkConnectivityLevel.InternetAccess)
            {
                return;
            }

            var deferral = taskInstance.GetDeferral();

            var settings   = new BackgroundSettingsViewModel();
            var dataSource = new MobileServicesDataSource(settings.MobileServicesUserId,
                                                          new AccessTokenRetriever(),
                                                          new BugSenseLogger());
            bool couldLogIn = await dataSource.Login(false, true);

            if (couldLogIn)
            {
                await dataSource.PushLocalChangesToServer();
            }

            deferral.Complete();
        }
Esempio n. 2
0
        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.");
        }
Esempio n. 3
0
        //
        // The Run method is the entry point of a background task.
        //
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            Debug.WriteLine("Background " + taskInstance.Task.Name + " Starting...");

            //
            // Associate a cancellation handler with the background task.
            //
            taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);

            try
            {
                NetworkStateChangeEventDetails details = taskInstance.TriggerDetails as NetworkStateChangeEventDetails;

                localSettings.Values["HasNewConnectionCost"]            = details.HasNewConnectionCost ? "New Connection Cost" : null;
                localSettings.Values["HasNewDomainConnectivityLevel"]   = details.HasNewDomainConnectivityLevel ? "New Domain Connectivity Level" : null;
                localSettings.Values["HasNewHostNameList"]              = details.HasNewHostNameList ? "New HostName List" : "";
                localSettings.Values["HasNewInternetConnectionProfile"] = details.HasNewInternetConnectionProfile ? "New Internet Connection Profile" : null;
                localSettings.Values["HasNewNetworkConnectivityLevel"]  = details.HasNewNetworkConnectivityLevel ? "New Network Connectivity Level" : null;

                ConnectionProfile profile = NetworkInformation.GetInternetConnectionProfile();
                if (profile == null)
                {
                    localSettings.Values["InternetProfile"]  = "Not connected to Internet";
                    localSettings.Values["NetworkAdapterId"] = "Not connected to Internet";
                }
                else
                {
                    localSettings.Values["InternetProfile"] = profile.ProfileName;

                    var networkAdapterInfo = profile.NetworkAdapter;
                    if (networkAdapterInfo == null)
                    {
                        localSettings.Values["NetworkAdapterId"] = "Not connected to Internet";
                    }
                    else
                    {
                        localSettings.Values["NetworkAdapterId"] = networkAdapterInfo.NetworkAdapterId.ToString();
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unhandled exception: " + e.ToString());
            }
        }