public static void RequestMonitor(object sender, ElapsedEventArgs e, string Prosper, MonitorCore.MMonitor mMonitor, TextBox txtArea) { try { var List = mMonitor.Monitor(ref Prosper); if (List.Count > 0) { foreach (var l in List) { if (mMonitor.Error) { txtArea.ForeColor = Color.Red; txtArea.Clear(); } txtArea.AppendText(l + " \r\n"); } } //intentos++; //if (intentos > 5) //{ // intentos = 0; // Prosper = null; //} } catch (Exception ex) { txtArea.ForeColor = Color.Red; txtArea.Text = ex.Message; txtArea.Clear(); } }
public static async Task RequestMonitor(TimeSpan interval, CancellationToken cancellationToken, MonitorCore.MMonitor mMonitor, TextBox txtArea) { string Prosper = null; int intentos = 0; int intentosConds = 0; bool last_error = false; txtArea.ForeColor = Color.Black; while (true) { try { if (cancellationToken.IsCancellationRequested) { break; } intentos++; await Task.Run(() => { try { mMonitor.Error = false; return(mMonitor.Monitor(ref Prosper)); } catch (Exception ex) { mMonitor.Error = true; return(new List <string>() { ex.Message }); } }).ContinueWith((response) => { var List = response.Result; if (last_error != mMonitor.Error) { Prosper = null; txtArea.Clear(); if (mMonitor.Error == false) { txtArea.ForeColor = Color.Black; } else { txtArea.ForeColor = Color.Red; } } if (List.Count > 0) { foreach (var l in List) { txtArea.AppendText(l + " \r\n"); } } last_error = mMonitor.Error; }, CancellationToken.None, TaskContinuationOptions.None, TaskScheduler.FromCurrentSynchronizationContext()); if (intentos > 3) { intentosConds++; intentos = 0; Prosper = null; } //if(intentosConds > 6) //{ // intentosConds = 0; // await Task.Run(()=> { // try // { // mMonitor.RequestConds(); // } // catch(Exception ex) // { // txtArea.ForeColor = Color.Red; // txtArea.AppendText(ex.Message); // txtArea.ForeColor = Color.Black; // } // }); //} await Task.Delay(interval, cancellationToken); } catch (Exception) { throw; //last_error = true; //txtArea.ForeColor = Color.Red; //txtArea.Text = "Error: " + ex.Message + "\r\n"; } } }