private async void DoButton_Click(object sender, RoutedEventArgs e) { var folderBrowserDialog = new FolderBrowserDialog() { Description = "データ保存先を選択してください" }; folderBrowserDialog.ShowDialog(); path = folderBrowserDialog.SelectedPath; if (string.IsNullOrEmpty(path)) { return; } if (DateTime.TryParse(StartTimeTextBox.Text, out var time)) { startTime = time; } else { MessageBox.Show("開始時刻の形式が正しくありません"); return; } if (int.TryParse(SyuyoEkiTimeTextBox.Text, out var n)) { syuyoDelayTime = n; } else { MessageBox.Show("時刻の形式が正しくありません"); return; } if (int.TryParse(TsuzyoEkiTimeTextBox.Text, out var m)) { DelayTime = m; } else { MessageBox.Show("時刻の形式が正しくありません"); return; } Lock(); LogText.WriteLine($"{path} にデータを保存します..."); var waitTime = (startTime - DateTime.Now); LogText.WriteLine($"{waitTime.ToString()}秒待機します..."); await Task.Delay(waitTime); LogText.WriteLine("指定時刻になりました。情報取得プロセスを開始します..."); cancellationTokenSource = new CancellationTokenSource(); await Task.WhenAll(GetSyuyoData(cancellationTokenSource.Token), GetAllData(cancellationTokenSource.Token), GetEreaData(cancellationTokenSource.Token)); }
private async Task DataGeter(CancellationToken cancellationToken, IEnumerable <int> id, string saveFolderPath) { while (true) { var savePath = saveFolderPath + $"\\{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}"; Directory.CreateDirectory(savePath); var saveFileWriter = new SaveFileWriter(savePath); await Task.Run(() => { Parallel.ForEach(id, async staId => { try { await saveFileWriter.SaveTrainData(staId); if (staId < 5) { LogText.WriteLine($"{DateTime.Now}: 広域データID{staId}を保存しました..."); } else { LogText.WriteLine($"{DateTime.Now}: {StationReader.GetStationById(staId).Name}を保存しました..."); } } catch (TrainDataGetException ex) { LogText.WriteLine("通信中に例外が発生しました..."); throw; } }); }); LogText.WriteLine("取得プロセス一時停止(取得完了)..."); await Task.Delay(DelayTime * 60 * 1000, cancellationToken); if (cancellationToken.IsCancellationRequested) { break; } } }