这个问题来自社区提问代码保留一份用来以后回答
using System;
using SystemComponentModel;
using SystemWindowsForms;
namespace WindowsApplication
{
/**//// /// gui 类
///
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
private void button_Click(object sender EventArgs e)
{
//用子线程工作
new SystemThreadingThread(new SystemThreadingThreadStart(StartDownload))
Start();
}
//开始下载
public void StartDownload()
{
Downloader downloader = new Downloader();
downloaderonDownLoadProgress += new DownloaderdDownloadProgress(downloader_
onDownLoadProgress);
downloaderStart();
}
//同步更新ui
void downloader_onDownLoadProgress(long total long current)
{
if (thisInvokeRequired)
{
thisInvoke(new DownloaderdDownloadProgress(downloader_onDownLoadProgress)
new object[] { total current });
}
else
{
thisprogressBarMaximum = (int)total;
thisprogressBarValue = (int)current;
}
}
}
/**//// /// 下载类
///
public class Downloader
{
//委托
public delegate void dDownloadProgress(long totallong current);
//事件
public event dDownloadProgress onDownLoadProgress;
//开始模拟工作
public void Start()
{
for (int i = ; i < ; i++)
{
if (onDownLoadProgress != null)
onDownLoadProgress( i);
SystemThreadingThreadSleep();
}
}
}
}