I'm tinkering away on a multithreaded downloader, using a producer/consumer queue construct; the downloading parts works fine, but I'm running into a problem keeping the GUI updated.
For now I'm using a listbox control on the form to display status messages and updates on downloading progress, eventually I hope to replace that with progressbars.
First the Form1 code behind; the form contains nothing but a button and the listbox:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void SetProgressMessage(string message)
{
if (listboxProgressMessages.InvokeRequired)
{
listboxProgressMessages.Invoke(new MethodInvoker(delegate()
{ SetProgressMessage(message); }));
}
else
{
listboxProgressMessages.Items.Add(message);
listboxProgressMessages.Update();
}
}
private void buttonDownload_Click(object sender, EventArgs e)
{
SetProgressMessage("Enqueueing tasks");
using (TaskQueue q = new TaskQueue(4))
{
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
q.EnqueueTask("url");
}
SetProgressMessage("All done!");
}
}
Now the producer/consumer logic. The consumer downloads the files bit by bit, and should tell the listbox living on the GUI thread how the progress is coming along; This works, but the listbox doesn't actually update until all is finished, also the messages appear after the 'All done!' message, which isn't desirable.
TaskQueue.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
using System.Windows.Forms;
using System.Runtime.Remoting.Messaging;
namespace WinformProducerConsumer
{
public class TaskQueue : IDisposable
{
object queuelocker = new object();
Thread[] workers;
Queue<string> taskQ = new Queue<string>();
public TaskQueue(int workerCount)
{
workers = new Thread[workerCount];
for (int i = 0; i < workerCount; i++)
(workers[i] = new Thread(Consume)).Start();
}
public void Dispose()
{
foreach (Thread worker in workers) EnqueueTask(null);
foreach (Thread worker in workers) worker.Join();
}
public void EnqueueTask(string task)
{
lock (queuelocker)
{
taskQ.Enqueue(task);
Monitor.PulseAll(queuelocker);
}
}
void Consume()
{
while (true)
{
string task;
Random random = new Random(1);
lock (queuelocker)
{
while (taskQ.Count == 0) Monitor.Wait(queuelocker);
task = taskQ.Dequeue();
}
if (task == null) return;
try
{
Uri url = new Uri(task);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
Int64 iSize = response.ContentLength;
Int64 iRunningByteTotal = 0;
using (WebClient client = new System.Net.WebClient())
{
using (Stream streamRemote = client.OpenRead(new Uri(task)))
{
using (Stream streamLocal = new FileStream(@"images\" + Path.GetFileName(task), FileMode.Create, FileAccess.Write, FileShare.None))
{
int iByteSize = 0;
byte[] byteBuffer = new byte[iSize];
while ((iByteSize = streamRemote.Read(byteBuffer, 0, byteBuffer.Length)) > 0)
{
streamLocal.Write(byteBuffer, 0, iByteSize);
iRunningByteTotal += iByteSize;
double dIndex = (double)iRunningByteTotal;
double dTotal = (double)byteBuffer.Length;
double dProgressPercentage = (dIndex / dTotal);
int iProgressPercentage = (int)(dProgressPercentage * 100);
string message = String.Format("Thread: {0} Done: {1}% File: {2}",
Thread.CurrentThread.ManagedThreadId,
iProgressPercentage,
task);
Form1 frm1 = (Form1)FindOpenForm(typeof(Form1));
frm1.BeginInvoke(new MethodInvoker(delegate()
{
frm1.SetProgressMessage(message);
}));
}
streamLocal.Close();
}
streamRemote.Close();
}
}
}
catch (Exception ex)
{
// Generate message for user
}
}
}
private static Form FindOpenForm(Type typ)
{
for (int i1 = 0; i1 < Application.OpenForms.Count; i1++)
{
if (!Application.OpenForms[i1].IsDisposed && (Application.OpenForms[i1].GetType() == typ))
{
return Application.OpenForms[i1];
}
}
return null;
}
}
}
Any suggestions, examples? I've looked around for solutions, but couldn't find anything that I could follow or worked.
Replacing the frm1.BeginInvoke(new MethodInvoker(delegate() with a frm1.Invoke(new MethodInvoker(delegate() results in a deadlock. I'm rather stumped here.
Sources: Producer/Consumer example: http://www.albahari.com/threading/part4.aspx
Update: I'm going about this the wrong way; instead of invoking back to the GUI from the worker threads, I'll use events that the GUI thread will have to keep an eye on. A lesson learned. :)