Ejemplo n.º 1
0
 private async void dataDownloadButton_Click(object sender, EventArgs e)
 {
     try
     {
         await System.Threading.Tasks.Task.Run(async() => await DataFeedDesktop.DownloadAsync(true, Settings.TimeoutDuration));
     }
     catch
     {
         MessageBox.Show(Settings.Localization.UnreachableHost, Settings.Localization.Offline, MessageBoxButtons.OK, MessageBoxIcon.Error);
         return;
     }
     MessageBox.Show(Settings.Localization.DataDownloadedSuccessfully);
 }
Ejemplo n.º 2
0
        private async void InitLoadingWindow_Shown(object sender, EventArgs e)
        {
            var gw = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);

            gw.TryStart(false, TimeSpan.FromSeconds(5));

            DataFeedClient.GeoWatcher = new Timetables.Utilities.CPGeolocator(() =>
            {
                var loc = gw.Position.Location;
                return(new Timetables.Utilities.Position(loc.Latitude, loc.Longitude));
            });

            topBarTimer.Start();

            System.Windows.Forms.Application.DoEvents();

            Preprocessor.DataFeed.LoadingProgress += LoadingProgressCallback;

            await Task.Run(async() =>
            {
                try
                {
                    var loadingThread = DataFeedDesktop.DownloadAsync(false, Settings.TimeoutDuration);
                    bool timerStarted = false;

                    try
                    {
                        do
                        {
                            if (loadingThread.IsFaulted)
                            {
                                throw loadingThread.Exception;
                            }

                            if (!timerStarted && (DataFeedDesktop.Downloaded || !DataFeedDesktop.OfflineMode))
                            {
                                timerStarted = true;
                            }

                            else if (timerStarted && loadingProgressBar.Value > 95)
                            {
                                break;
                            }

                            else if (timerStarted && loadingProgressBar.Value < 100)
                            {
                                Thread.Sleep(30);
                                LoadingProgressCallback("Loading data.", 1);
                            }
                        }while (!DataFeedDesktop.Loaded);
                    }

                    catch (AggregateException ex)
                    {
                        foreach (var innerEx in ex.InnerExceptions)
                        {
                            if (innerEx is System.Net.WebException)
                            {
                                MessageBox.Show(Settings.Localization.UnreachableHost, Settings.Localization.Offline, MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }

                            else
                            {
                                Environment.Exit(1);
                            }

                            IsFaulted = true;
                        }
                    }

                    finally
                    {
                        await loadingThread;

                        // Just for an effect :-)

                        while (loadingProgressBar.Value < 100)
                        {
                            Thread.Sleep(30);
                            LoadingProgressCallback("Loading data.", 1);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + "Cannot continue in initialization.");
                    Environment.Exit(1);
                }
            });


            Preprocessor.DataFeed.LoadingProgress -= LoadingProgressCallback;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            if (!DataFeedDesktop.OfflineMode)
            {
                Request.UpdateCachedResultsAsync();
            }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed

            Close();
        }