.NET 4.x Как остановить цикл в потоке без зависания формы - C#
Формулировка задачи:
Здравствуйте
Есть цикл, в цикле при определенных условиях начинается скачка файла, скачка начинается в отдельном потоке, поэтому чтобы приостановить цикл использую t.Join, но форма все равно зависает, что я делаю не так?
Спасибо.
update!
Скриншот:
Решение задачи: «.NET 4.x Как остановить цикл в потоке без зависания формы»
textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Cache;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
WebClient webClient = new WebClient();
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(webClient_DownloadFileCompleted);
webClient.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
webClient.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
webClient.DownloadFileAsync(new Uri("http://mc.vincere.in/update/list.txt"), "list.txt");
}
void webClient_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
progressBar1.Value = 100;
button1.Enabled = true;
}
void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
}
}