私は C# でのプログラミングが初めてで、簡単な解決策を探しています。フォームに 2 つのボタンがあり、1 つは DownloadFileAsync() を呼び出しており、2 つ目はこの操作をキャンセルする必要があります。最初のボタンのコード:
private void button1_Click(object sender, EventArgs e)
{
...
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri(textBox1.Text), destination);
}
2 番目のボタンのコード:
private void button2_Click(object sender, EventArgs e)
{
webClient.CancelAsync(); // yes, sure, WebClient is not known here.
}
この問題をすばやく解決する方法を探しています (最初の関数の webClient を 2 番目のブロックで使用します)。