/// <summary>Initializes the TimeService and waits for a valid time from the server</summary> /// <param name="timeServiceName">Host name of the time service server to use</param> /// <param name="timeZoneOffset">Timezone offset (in minutes) to use for the service</param> public static void InitTimeService(string timeServiceName, int timeZoneOffset) { // Get the address for the time service. // It is considered bad practice to hard code the address // as the service will likely load balance to a variety // of physical IP addresses. IPHostEntry entry = Dns.GetHostEntry(timeServiceName); if (entry == null || entry.AddressList == null) { throw new ApplicationException("DNS failure"); } // It is possible (at least on the emulator) that // an address in the list may be null, so loop // over them until a valid one is found. IPAddress timeServiceAddress = null; for (int i = 0; i < entry.AddressList.Length; ++i) { timeServiceAddress = entry.AddressList[i]; if (timeServiceAddress != null) { break; } } // need to have a valid one to continue if (timeServiceAddress == null) { throw new ApplicationException("DNS failure"); } TimeService.Settings = new TimeServiceSettings { PrimaryServer = timeServiceAddress.GetAddressBytes( ) , RefreshTime = 10 , AutoDayLightSavings = true }; TimeService.SetTimeZoneOffset(timeZoneOffset); // Start the service and wait for initial time update/sync from server TimeService.SystemTimeChanged += TimeService_SystemTimeChanged; TimeService.Start( ); TimeChanged.WaitOne( ); TimeService.SystemTimeChanged -= TimeService_SystemTimeChanged; }