public void Stop() { if (cts == null) { return; } cts.Cancel(); Status = ProviderStatusEnum.Idle; cts = null; }
public void Start() { if (string.IsNullOrEmpty(city) || string.IsNullOrEmpty(key)) { return; } Stop(); cts = new CancellationTokenSource(); Task.Factory.StartNew(async() => { TimeSpan delay = TimeSpan.FromMinutes(10); var token = cts.Token; try { while (true) { token.ThrowIfCancellationRequested(); string url = string.Format(serviceAPI, key, city); var data = await loadData(url);//load forcast if (data.Status != ProviderStatusEnum.NA) { forcast = data; } url = string.Format(serviceAPI_Current, key, city); data = await loadData(url);//load current weather if (data.Status != ProviderStatusEnum.NA) { current = data; } await Task.Delay(delay, token); } } catch (OperationCanceledException) { System.Diagnostics.Debug.WriteLine("Xinzhi Weather operation canceled"); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.ToString()); } }, cts.Token ); Status = ProviderStatusEnum.Ready; }
public XinzhiWeatherForcast(string key, string defaultCity) { city = defaultCity; this.key = key; Status = ProviderStatusEnum.Idle; }