Esempio n. 1
0
        private static async Task <DepartureBoardResponse> SendDepartureBoardRequestAsync(LineInfoRequest dbRequest, bool forceCache = false)
        {
            DepartureBoardResponse dbResponse = null;

            using (var dbProcessing = new DepartureBoardProcessing())
            {
                var cached = LineInfoCached.Select(dbRequest.RouteInfoID);

                if (cached == null || (cached.ShouldBeUpdated || forceCache))
                {
                    try
                    {
                        if (!await CheckBasicDataValidity())
                        {
                            var results = cached?.FindResultsSatisfyingRequest(dbRequest);
                            return(results?.Departures.Count == 0 ? null : results);
                        }

                        // Process the request immediately so the user does not have to wait until the caching is completed.

                        dbResponse = await dbProcessing.ProcessAsync(dbRequest, dbRequest.Count == -1?int.MaxValue : Settings.TimeoutDuration);

                        // Then update the cache.

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        if (cached != null && cached.ShouldBeUpdated && dbRequest.Count != -1 && CanBeCached)
                        {
                            Task.Run(async() => cached.UpdateCache(await dbProcessing.ProcessAsync(cached.ConstructNewRequest(), int.MaxValue)));
                        }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                    }
                    catch (System.Net.WebException)
                    {
                        PlatformDependentSettings.ShowMessage(Settings.Localization.UnreachableHost);
                    }
                }

                else
                {
                    dbResponse = cached?.FindResultsSatisfyingRequest(dbRequest);
                    if (dbResponse?.Departures.Count == 0)
                    {
                        dbResponse = await dbProcessing.ProcessAsync(dbRequest, Settings.TimeoutDuration);
                    }
                }
            }

            return(dbResponse);
        }
Esempio n. 2
0
        private static async Task <DepartureBoardResponse> SendDepartureBoardRequestAsync(StationInfoRequest dbRequest, bool forceCache = false)
        {
            DepartureBoardResponse dbResponse = null;

            if (DataFeedDesktop.OfflineMode)
            {
                await Task.Run(() =>
                {
                    using (var dbProcessing = new Interop.DepartureBoardManaged(DataFeedDesktop.Full, dbRequest))
                    {
                        dbProcessing.ObtainDepartureBoard();
                        dbResponse = dbProcessing.ShowDepartureBoard();
                    }
                });
            }

            else
            {
                using (var dbProcessing = new DepartureBoardProcessing())
                {
                    var cached = StationInfoCached.Select(dbRequest.StopID);

                    if (cached == null || (cached.ShouldBeUpdated || forceCache))
                    {
                        try
                        {
                            if (!await CheckBasicDataValidity())
                            {
                                var results = cached?.FindResultsSatisfyingRequest(dbRequest);
                                return(results?.Departures.Count == 0 ? null : results);
                            }

                            // Process the request immediately so the user does not have to wait until the caching is completed.

                            dbResponse = await dbProcessing.ProcessAsync(dbRequest, dbRequest.Count == -1?int.MaxValue : Settings.TimeoutDuration);

                            // Then update the cache.

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                            if (cached != null && cached.ShouldBeUpdated && dbRequest.Count != -1)
                            {
                                Task.Run(async() => cached.UpdateCache(await dbProcessing.ProcessAsync(cached.ConstructNewRequest(), int.MaxValue)));
                            }
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                        }
                        catch (System.Net.WebException)
                        {
                            MessageBox.Show(Settings.Localization.UnreachableHost, Settings.Localization.Offline, MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }

                    else
                    {
                        dbResponse = cached?.FindResultsSatisfyingRequest(dbRequest);
                        if (dbResponse?.Departures.Count == 0)
                        {
                            dbResponse = await dbProcessing.ProcessAsync(dbRequest, Settings.TimeoutDuration);
                        }
                    }
                }
            }
            return(dbResponse);
        }