/* ----------------------------------------------------------------- */ /// /// BackgroundWorker_DoWork /// /// <summary> /// コンボボックスで指定されている電力会社の電力状況を取得します。 /// </summary> /// /* ----------------------------------------------------------------- */ private void BackgroundWorker_DoWork(Object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; if (worker == null) return; try { Client client = null; switch (this._setting.TargetArea) { case Area.Chubu: client = new ChubuClient(); break; case Area.Chugoku: client = new ChugokuClient(); break; case Area.Hokkaido: client = new HokkaidoClient(); break; case Area.Hokuriku: client = new HokurikuClient(); break; case Area.Kansai: client = new KansaiClient(); break; case Area.Kyushu: client = new KyushuClient(); break; case Area.Shikoku: client = new ShikokuClient(); break; case Area.Tohoku: client = new TohokuClient(); break; case Area.Tokyo: client = new TokyoClient(); break; } var interval = new TimeSpan(0, 10, 0); while (true) { worker.ReportProgress(0, "最新の情報を取得中です..."); var response = client.GetResponse(DateTime.Now); if (response != null) worker.ReportProgress(100, response); else worker.ReportProgress(0, "電力使用状況を取得できませんでした"); var latest = DateTime.Now; while (DateTime.Now - latest < interval) { if (worker.CancellationPending) { e.Cancel = true; return; } System.Threading.Thread.Sleep(100); } } } catch (Exception err) { worker.ReportProgress(0, err.Message); } }
/* ----------------------------------------------------------------- */ // BackgroundWorker 関連のメソッド群 /* ----------------------------------------------------------------- */ #region Background workder's methods /* ----------------------------------------------------------------- */ /// /// BackgroundWorker_DoWork /// /// <summary> /// コンボボックスで指定されている電力会社の電力状況を取得します。 /// </summary> /// /* ----------------------------------------------------------------- */ private void BackgroundWorker_DoWork(Object sender, DoWorkEventArgs e) { var worker = sender as BackgroundWorker; if (worker == null) { return; } try { Client client = null; switch (this._setting.TargetArea) { case Area.Chubu: client = new ChubuClient(); break; case Area.Chugoku: client = new ChugokuClient(); break; case Area.Hokkaido: client = new HokkaidoClient(); break; case Area.Hokuriku: client = new HokurikuClient(); break; case Area.Kansai: client = new KansaiClient(); break; case Area.Kyushu: client = new KyushuClient(); break; case Area.Shikoku: client = new ShikokuClient(); break; case Area.Tohoku: client = new TohokuClient(); break; case Area.Tokyo: client = new TokyoClient(); break; } var interval = new TimeSpan(0, 10, 0); while (true) { worker.ReportProgress(0, "最新の情報を取得中です..."); var response = client.GetResponse(DateTime.Now); if (response != null) { worker.ReportProgress(100, response); } else { worker.ReportProgress(0, "電力使用状況を取得できませんでした"); } var latest = DateTime.Now; while (DateTime.Now - latest < interval) { if (worker.CancellationPending) { e.Cancel = true; return; } System.Threading.Thread.Sleep(100); } } } catch (Exception err) { worker.ReportProgress(0, err.Message); } }