This question comes from a community question, and the code keeps a copy for later answers.
Using System;
Using System.ComponentModel;
Using System.Windows.Forms;
Namespace WindowsApplication4
... {
/**////<summary>
GUI class
</summary>
public partial class Form1:form
... {
Public Form1 ()
... {
InitializeComponent ();
}
private void Button1_Click (object sender, EventArgs e)
... {
Working with child threads
New System.Threading.Thread (New System.Threading.ThreadStart (Startdownload)). Start ();
}
Start download
public void Startdownload ()
... {
Downloader downloader = new Downloader ();
Downloader.ondownloadprogress + = new downloader.ddownloadprogress (downloader_ondownloadprogress);
Downloader. Start ();
}
Synchronizing Update UI
void Downloader_ondownloadprogress (long, long)
... {
if (this. invokerequired)
... {
This. Invoke (New Downloader.ddownloadprogress (downloader_ondownloadprogress), new object[] ... {total, current});
}
Else
... {
This.progressBar1.Maximum = (int) total;
This.progressBar1.Value = (int) current;
}
}
}
/**////<summary>
Download class
</summary>
public Class Downloader
... {
Commissioned
public delegate void Ddownloadprogress (long total,long current);
Event
public event ddownloadprogress Ondownloadprogress;
Start simulation work
public void Start ()
... {
for (int i = 0; i < i++)
... {
if (ondownloadprogress!= null)
Ondownloadprogress (i);
System.Threading.Thread.Sleep (100);
}
}
}
}