static void Main(string[] args) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; Console.Title = "ETH-GasOMeter by lwYeo (2018)"; Console.CancelKeyPress += Console_CancelKeyPress; _ManualResetEvent = new ManualResetEvent(false); _Args = GetArguments(args); while (_Instance == null) { try { _Instance = new EthGasOMeter(_Args["web3-url"], _Args["recent-blocks"], _Args["loop-delay"], bool.Parse(_Args["enable-ethgasstation"])); _Instance.OnMessage += _OnMessage; _Instance.OnEthGasStation += _Instance_OnEthGasStation; _Instance.Transaction.OnBlockEvent += _Transaction_OnBlockEvent; _Instance.OnRequestUserInput += _Instance_OnRequestUserInput; _Instance.Start(_Args["to-address"]); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine(); try { _Instance.Dispose(); } catch { } _Instance = null; } } try { _APIService = new ApiService(); _APIService.OnMessage += _OnMessage; _APIService.OnAPIResponse += _APIService_OnAPIResponse; _APIService.Start(_Args["api-bind"]); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (NotSupportedException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } _ManualResetEvent.WaitOne(); if (_APIService != null) { _APIService.Stop(); _APIService.Dispose(); } Environment.Exit(0); }
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e) { Console.WriteLine("Ctrl-C was pressed."); e.Cancel = true; if (_IsCancelKeyPressed) { _ManualResetEvent.Set(); Environment.Exit(0); } _IsCancelKeyPressed = true; if (_APIService != null) { _APIService.Stop(); _APIService.OnMessage -= _OnMessage; _APIService.OnAPIResponse -= _APIService_OnAPIResponse; _APIService.Dispose(); } _Instance.OnMessage -= _OnMessage; _Instance.OnEthGasStation -= _Instance_OnEthGasStation; _Instance.Transaction.OnBlockEvent -= _Transaction_OnBlockEvent; _Instance.OnRequestUserInput -= _Instance_OnRequestUserInput; _Instance.Dispose(); _Instance = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true); while (_Instance == null) { try { _Instance = new EthGasOMeter(_Args["web3-url"], _Args["recent-blocks"], _Args["loop-delay"], bool.Parse(_Args["enable-ethgasstation"])); _Instance.OnMessage += _OnMessage; _Instance.OnEthGasStation += _Instance_OnEthGasStation; _Instance.Transaction.OnBlockEvent += _Transaction_OnBlockEvent; _Instance.OnRequestUserInput += _Instance_OnRequestUserInput; Task.Run(() => { _Instance.Start(null, showCancel: true); }). ContinueWith((task) => { try { _APIService = new ApiService(); _APIService.OnMessage += _OnMessage; _APIService.OnAPIResponse += _APIService_OnAPIResponse; _APIService.Start(_Args["api-bind"]); } catch (ArgumentException ex) { Console.WriteLine(ex.Message); } catch (NotSupportedException ex) { Console.WriteLine(ex.Message); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } _IsCancelKeyPressed = false; }); } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine(); try { _Instance.Dispose(); } catch { } _Instance = null; } } }