Exemple #1
0
        private static void Main(String[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException);
            try
            {
                Application.Run(new MainForm(args));
            }
            catch (Exception exception)
            {
                try
                {
                    if (MessageForm.GetDialogResultOnException(exception,
                                                               Globals.Localization.CriticalExceptionOccurredText,
                                                               Globals.Localization.CriticalExceptionOccurred, MessageBoxButtons.RetryCancel,
                                                               new[]
                    {
                        Globals.Localization.CriticalExceptionOccurredRestartButton,
                        Globals.Localization.CriticalExceptionOccurredExitButton
                    }) == DialogResult.Retry)
                    {
                        Application.Restart();
                    }
                }
                catch (Exception)
                {
                    try
                    {
                        MessageForm.GetDialogResultOnException(exception, Globals.Localization.InitializeStageException,
                                                               Globals.Localization.InitializeExceptionOccured, MessageBoxButtons.OK, new[] { Globals.Localization.Exit });
                    }
                    catch (Exception)
                    {
                        MessageForm.GetDialogResultOnException(exception, "Exception on initialize stage",
                                                               "Initialize exception occured", MessageBoxButtons.OK, new[] { "Exit" });
                    }
                }

                Environment.Exit(exception.HResult);
            }
        }
        private async Task DownloadImageAsync(Image image)
        {
            try
            {
                if (IsInvalid || _token.IsCancellationRequested)
                {
                    return;
                }

                await WaitAsync(_token).ConfigureAwait(true);

                String formatedSavePath = null;
                if (SaveToDisk)
                {
                    formatedSavePath = GetFormatedFilePath(image);

                    if (String.IsNullOrEmpty(formatedSavePath))
                    {
                        Log.Add(new LogMessage(Globals.Localization.FormatFileNameError,
                                               MessageType.Warning, new[] { image.id.ToString() }));
                        ImageDownloaded?.Invoke(image);
                        ImageSaved?.Invoke(image);
                        return;
                    }

                    if (File.Exists(formatedSavePath) && !Globals.ExistFileRewrite.GetValue())
                    {
                        ImageDownloaded?.Invoke(image);
                        ImageSaved?.Invoke(image);
                        return;
                    }
                }

                Byte[] imageByte = await GetImageAsync(image).ConfigureAwait(true);

                if (_token.IsCancellationRequested || imageByte == null)
                {
                    return;
                }

                ImageDownloaded?.Invoke(image);

                if (SaveToDisk)
                {
                    try
                    {
                        await SaveImageAsync(image, imageByte, formatedSavePath).ConfigureAwait(true);
                    }
                    catch (Exception e)
                    {
                        Log.Add(new LogMessage(e.Message, MessageType.CriticalWarning));
                        IsInvalid = true;
                        MessageForm.GetDialogResultOnException(e, null, null, MessageBoxButtons.OK, new[] { Globals.Localization.Accept });
                    }
                }
            }
            catch (Exception e)
            {
                Log.Add(new LogMessage($"{e.Message}{LocalizationBase.NewLine}{e.StackTrace}", MessageType.CriticalWarning));
                throw;
            }
        }