private static void TryWrite(object sender, ElapsedEventArgs e, BoundsCapturer b) { int[] values = b.GetValuesFromBound(); if (values == null) { ToFile("Bad values, skipping"); return; } try { var response = PushData(values); ToFile(JsonConvert.SerializeObject(response)); if (response != null && response.TotalUpdatedCells != null && response.TotalUpdatedCells > 0) { Pusher.Stop(); Adjutant.Stop(); Adjutant.Interval = (DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds; Adjutant.Start(); } } catch (Exception ex) { ToFile(ex.Message); ToFile("Config file uninitialized"); Environment.Exit(1); } }
static void Main(string[] args) { var waitHandle = new EventWaitHandle(false, EventResetMode.AutoReset, "GFL-OCR-RSC-Tracker", out bool createdNew); var signaled = false; if (!createdNew) { waitHandle.Set(); return; } LogFile = "logs\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".txt"; Directory.CreateDirectory("logs"); File.Create(LogFile).Dispose(); cfg = TrackerConfig.GetConfig(); BoundsCapturer b = new BoundsCapturer(); Application.EnableVisualStyles(); int[] values = null; bool accepted = false; while (!accepted) { values = null; while (values == null) { b.CreateFinder(); ToFile(b.GeneratedBounds.ToString()); ToFile("Starting parsing"); values = b.GetValuesFromBound(); if (values == null) { ToFile("Couldn't find values, reopening capture window"); if (MessageBox.Show("Could not find resource values, retry capture?", "Error finding values", MessageBoxButtons.YesNo) == DialogResult.No) { Environment.Exit(0); } } } DialogResult r = MessageBox.Show("Manpower\tAmmo\t\tRations\t\tParts\n" + values[0] + "\t\t" + values[1] + "\t\t" + values[2] + "\t\t" + values[3] + "\n\nDo these values look correct?", "Value Confirmation", MessageBoxButtons.YesNoCancel); if (r == DialogResult.Cancel) { Environment.Exit(0); } accepted = r == DialogResult.Yes; } Pusher = new System.Timers.Timer(); Pusher.Elapsed += (s, a) => TryWrite(s, a, b); Pusher.Interval = 1000 * 60 * 1; //1 minute interval Adjutant = new System.Timers.Timer(); Adjutant.Elapsed += StartTrying; Adjutant.Interval = (DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds; //runs midnight tomorrow Adjutant.AutoReset = false; Adjutant.Start(); TryWrite(null, null, b); do { signaled = waitHandle.WaitOne(); } while (!signaled); }