Ejemplo n.º 1
0
        private NetworkDetector()
        {
            requestQueue = new Queue<long>();
            requestQueue.Clear();
            requestStatus = NetworkTypeRequestStatus.Default;
            updateTimer = new System.Windows.Threading.DispatcherTimer();
            updateTimer.Tick += new EventHandler(updateTimer_Tick);
            updateTimer.Interval = new TimeSpan(0, 0, 0, 0, 300);//there is no need to restart the BGWorker sooner then 300 millisec because the ~3request/sec requestlimit
            //updateTimer.Start();
            pollTimer = new System.Windows.Threading.DispatcherTimer();
            pollTimer.Tick += new EventHandler(pollTimer_Tick);
            networkWorker = new BackgroundWorker();
            networkWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(networkWorker_RunWorkerCompleted);
            networkWorker.DoWork += new DoWorkEventHandler(networkWorker_DoWork);
            IsInstantRequestPresent = false;
            detailedMode = false; //by default I hide the framework events for better Developer experience
            isZuneConnected = false;

            SetupNetworkChange();  //signing on the framework event
        }
Ejemplo n.º 2
0
 void updateTimer_Tick(object sender, EventArgs e)
 {
     if (requestQueue.Count > 0)
     {
         if (!networkWorker.IsBusy)
         {
             requestStatus = NetworkTypeRequestStatus.Started;
             networkWorker.RunWorkerAsync();
         }
     }
 }
Ejemplo n.º 3
0
 public void AsyncGetNetworkType()
 {
     //requestQueue.Enqueue(System.DateTime.Now.Ticks);
     IsInstantRequestPresent = true;
     if (!networkWorker.IsBusy)
     {
         requestStatus = NetworkTypeRequestStatus.Started;
         networkWorker.RunWorkerAsync();
     }
     if (!updateTimer.IsEnabled) updateTimer.Start();
 }
Ejemplo n.º 4
0
        //no need to lock the variables if We do everithing in the completed event handler

        void networkWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            DetectOnlineStatus();
            if ((detailedMode) || (net != (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result))
            {
                //there is no need to get events all the time, just when really changing something or the DetailedMode is true
                if (net != (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result)
                    RaiseNotify(OnLostNetworkType, new NetworkDetectorEventArgs(online, net));
                net = (Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType)e.Result;
                Debug.WriteLine("      New NetType: " + net.ToString());
                if (net == Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet) Debug.WriteLine("!!!!! Zune is Connected");
                switch (net)
                {
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet:
                        if (!isZuneConnected)
                        {
                            isZuneConnected = true;
                            RaiseNotify(OnZuneConnected, new NetworkDetectorEventArgs(online, net));
                        }
                        RaiseNotify(OnConnectedEthernet, new NetworkDetectorEventArgs(online, net));
                        break;
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Wireless80211:
                        if (isZuneConnected)
                        {
                            isZuneConnected = false;
                            RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net));
                        }
                        RaiseNotify(OnConnectedWifi, new NetworkDetectorEventArgs(online, net));
                        break;
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandCdma:
                        if (isZuneConnected)
                        {
                            isZuneConnected = false;
                            RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net));
                        }
                        RaiseNotify(OnConnectedBroadbandCdma, new NetworkDetectorEventArgs(online, net));
                        break;
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MobileBroadbandGsm:
                        if (isZuneConnected)
                        {
                            isZuneConnected = false;
                            RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net));
                        }
                        RaiseNotify(OnConnectedBroadbandGsm, new NetworkDetectorEventArgs(online, net));
                        break;
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.None:
                        if (!online)
                        {
                            if (isZuneConnected)
                            {
                                /*if we lost all network connection and the Zune was present before, 
                                 then we lost the Zune too. Normally when Zune is present and then we got
                                 a None NetType, the PC just lost the internet connection but not the Zune sync*/
                                isZuneConnected = false;
                                RaiseNotify(OnZuneDisconnected, new NetworkDetectorEventArgs(online, net));
                            }
                        }
                        RaiseNotify(OnConnectedNone, new NetworkDetectorEventArgs(online, net));
                        break;
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.AsymmetricDsl:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Atm:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.BasicIsdn:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ethernet3Megabit:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.FastEthernetFx:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.FastEthernetT:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Fddi:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.GenericModem:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.GigabitEthernet:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.HighPerformanceSerialBus:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.IPOverAtm:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Isdn:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Loopback:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.MultiRateSymmetricDsl:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Ppp:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.PrimaryIsdn:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.RateAdaptDsl:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Slip:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.SymmetricDsl:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.TokenRing:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Tunnel:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.Unknown:
                    case Microsoft.Phone.Net.NetworkInformation.NetworkInterfaceType.VeryHighSpeedDsl:
                    default://theoretically we can't get here but better be prepared
                        RaiseNotify(OnConnectedOther, new NetworkDetectorEventArgs(online, net));
                        break;
                }
            }
            for (int i = 0; i < requestQueue.Count; i++)
            {
                if (requestQueue.Peek() < System.DateTime.Now.Ticks) requestQueue.Dequeue();
                //the requests before this moment just got answered
                //if other requests are coming right after this, they will be served inside the next networkWorker_RunWorkerCompleted
            }
            if (requestQueue.Count == 0) updateTimer.Stop();
            if (IsInstantRequestPresent)  //the user requested a single poll
            {
                RaiseNotify(OnAsyncGetNetworkTypeCompleted, new NetworkDetectorEventArgs(online, net));
                IsInstantRequestPresent = false;
            }
            requestStatus = NetworkTypeRequestStatus.Ended;
            Debug.WriteLine("<<<<< GetNetType ended " + System.DateTime.Now.ToString("T") + " <<<<<");
        }