Ejemplo n.º 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            _curencyDb     = new CurrencyDbQueries();
            _curencyDbLob  = new CurrencyLogDbQueries();
            _updateService = new ExchangeWebService(_curencyDb, _curencyDbLob);
            _updateService.EventSuccess = (moead, log) =>
            {
                var txt = "{0} =>>>> {1} = {2:0.00}".ToFormat(moead.Code, moead.ExchangeBaseCurrencyCode, log.Value);
                //MessageBox.Show();
                iconeSistema.ShowBalloonTip(1000, "Currency Update", txt, ToolTipIcon.Info);
            };
            tabelaMoedas.DataSource = _curencyDb.List();
            tabelaMoedas.CellClick += (obj, op) =>
            {
                var moeda = (obj as DataGridView).CurrentRow.DataBoundItem as Currency;
                new GraficoCurrencyForm(moeda, _curencyDbLob.GetFromCurrency(moeda)).Show();
            };
            _thread = new Thread(async() =>
            {
                while (true)
                {
                    await _updateService.Run();

                    Action action = () =>
                    {
                        tabelaMoedas.DataSource = null;
                        tabelaMoedas.DataSource = _curencyDb.List();
                    };

                    if (this.Visible)
                    {
                        this.BeginInvoke(action);
                    }
                    Thread.Sleep(Constantes.IntervaloAtualizar);
                }
            });
            _thread.Start();
        }
Ejemplo n.º 2
0
        private async Task UpdateValuesOfCurrencies()
        {
            var web = new HttpClient();

            var moedas = _currencyDbQueries.List();

            foreach (var moeda in moedas)
            {
                try
                {
                    var response = await web.GetAsync(Constantes.GetExchange.ToFormat(moeda.Code, moeda.ExchangeBaseCurrencyCode));

                    var txt = await response.Content.ReadAsStringAsync();

                    var valor = txt.Split(new[] { "uccResultAmount'>" }, StringSplitOptions.None)[1].Split('<')[0].Replace('.', ',').ToDecimal();

                    if (valor == 0)
                    {
                        valor = await GetCryptoCurrencyValue(moeda.Code, moeda.ExchangeBaseCurrencyCode);
                    }

                    moeda.ExchageValue = valor;
                    moeda.LastUpdate   = DateTime.Now;
                    var log = new CurrencyLog
                    {
                        CurrencyId = moeda.Id,
                        Date       = moeda.LastUpdate,
                        Value      = valor
                    };
                    _currencyDbQueries.Update(moeda);
                    _currencyLogDbQueries.Insert(log);
                    EventSuccess(moeda, log);
                }
                catch (Exception ex)
                {
                }
            }
        }