If you paste the code below into a winform called Form1 and change <add web
address> to a valid web address, the code sample will ayncronously gradually
fill a DataGridView with rows as the web content is downloaded.
You need to change the download code and business object to suit your needs.
public partial class Form1 : Form
{
public Form4()
{
InitializeComponent();
}
BackgroundWorker worker = new BackgroundWorker();
protected override void OnLoad(EventArgs e)
{
DataGridView dataGridView1 = new DataGridView();
Controls.Add(dataGridView1);
dataGridView1.DataSource = list;
// Set up the BackgroundWorker
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.WorkerSupportsCancellation = true;
// Start the backgroundworker.
worker.RunWorkerAsync();
}
protected override void OnClosing(CancelEventArgs e)
{
// Notify the backgroundworker that the program is shutting down
// If you don't do this you will end up trying to update a
DataGridView that no longer exists.
if (worker.IsBusy)
worker.CancelAsync();
}
// This method will run asyncronously when worker is started
// Anything if you need to update the GUI thread from this method
you need to invoke.
void worker_DoWork(object sender, DoWorkEventArgs e)
{
// Create a request and response object to the uri
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(<add
web address>);
// If you need to go through a proxy, add necessary credentials
req.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
// Get the stream returned from the web site
using (Stream s = resp.GetResponseStream())
{
byte[] data = new byte[8192];
// The memorystream is just holding the entire download.
You may not need it.
int i = 0;
int pos = 0;
// While there is data
while ((i = s.Read(data, 0, data.Length)) > 0)
{
// Check to see if the program has shut down
if (worker.CancellationPending)
return;
// Add a log entry
AddLogEntry("Title", Encoding.Default.GetString(data));
pos += i;
// Simulate lengty procedure by sleeping 1 second
Thread.Sleep(1000);
}
}
}
// This delegate will hold necessary data to be able to invoke
AddLogEntry.
// The parameter types must mach the called method.
delegate void StringStringDelegate(string title, string text);
// This method will add a LogEntry object to 'list'
public void AddLogEntry(string title, string text)
{
// Since we are receiving calls from a non GUI thread pass the
call to the GUI thread using a delegate
if(InvokeRequired)
{
this.Invoke(new StringStringDelegate(AddLogEntry), new
object[]{title, text});
return;
}
list.Add(new LogEntry(title, text));
}
// A bindinglist will notify the DataGridView when it has new items
BindingList<LogEntry> list = new BindingList<LogEntry>();
// The business object to be listed in the DataGridView
class LogEntry
{
private string _title;
public string Title
{
get { return _title; }
set { _title = value; }
}
private string _text;
public string Text
{
get { return _text; }
set { _text = value; }
}
public LogEntry(string title, string text)
{
this.Title = title;
this.Text = text;
}
}
}