public async Task StartOrStop() { while (true) { await Task.Delay(1000); try { var client = new RestClient(_host); var request = new RestRequest($"GetBotStatus/{Properties.Settings.Default.Key}"); var res = await client.ExecuteAsync(request); var isOn = bool.Parse(res.Content); if (isOn) { if (!isStart) { if (!IsInitial) { BackgroundScheduler.Start(); startStop_btn.BackColor = Color.Green; IsInitial = true; } else { await BackgroundScheduler.Scheduler.ResumeAll(); startStop_btn.BackColor = Color.Green; isStart = true; } } } else if (isStart) { await BackgroundScheduler.Scheduler.PauseAll(); startStop_btn.BackColor = Color.Red; isStart = false; } } catch { } } }
private async void startStop_btn_Click(object sender, EventArgs e) { if (!IsInitial) { BackgroundScheduler.Start(); startStop_btn.BackColor = Color.Green; IsInitial = true; } else if (isStart) { await BackgroundScheduler.Scheduler.PauseAll(); startStop_btn.BackColor = Color.Red; isStart = false; } else { await BackgroundScheduler.Scheduler.ResumeAll(); startStop_btn.BackColor = Color.Green; isStart = true; } }