This repository has been archived on 2022-08-05. You can view files and clone it, but cannot push or open issues or pull requests.
UpTool2/UpTool2/Tool/DownloadDialog.cs

43 lines
1.2 KiB
C#
Raw Normal View History

2019-09-10 12:06:15 +02:00
using System;
using System.Net;
using System.Windows.Forms;
2020-02-28 12:59:59 +01:00
namespace UpTool2.Tool
2019-09-10 12:06:15 +02:00
{
public partial class DownloadDialog : Form
{
2020-02-28 14:26:16 +01:00
private readonly WebClient _client;
private bool _close;
public byte[] Result;
2020-02-28 12:59:59 +01:00
public DownloadDialog(string uri)
2019-09-10 12:06:15 +02:00
{
InitializeComponent();
try
{
2020-02-28 14:26:16 +01:00
_client = new WebClient();
_client.DownloadProgressChanged += ProgressChanged;
_client.DownloadDataCompleted += Done;
_client.DownloadDataAsync(new Uri(uri), _client);
2019-09-10 12:06:15 +02:00
}
catch
{
DialogResult = DialogResult.Abort;
Close();
}
}
2020-02-28 14:26:16 +01:00
private void Done(object sender, DownloadDataCompletedEventArgs e)
2019-09-10 12:06:15 +02:00
{
DialogResult = DialogResult.OK;
2020-02-28 14:26:16 +01:00
_close = true;
Result = e.Result;
2019-09-10 12:06:15 +02:00
Close();
}
2020-02-28 14:26:16 +01:00
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e) =>
progressBar.Value = e.ProgressPercentage;
2019-09-10 12:06:15 +02:00
2020-02-28 14:26:16 +01:00
private void DownloadDialog_FormClosing(object sender, FormClosingEventArgs e) => e.Cancel = !_close;
2019-09-10 12:06:15 +02:00
}
2020-02-28 12:59:59 +01:00
}