asdf87

I've been looking for a resumable download class, and I've found one here:

http://www.codeproject.com/cs/library/downloader.asp


My question focuses on this piece of code:
static void downloader_ProgressChanged(object sender, DownloadEventArgs e)
{
  // DownloadEventArgs has lots of progress information
  Console.WriteLine("Progress " + e.PercentDone);
}

I need to get the e.PercentDone value assigned to a progress bar's value.


Re: Visual C# Language Threading/scope question, passing values from thread A to thread B

ScubaSteve20001

I'm not sure of your question. Why can't you just set the progress bars 'Value' property to e.PercentDone. Unless of course the progress bar was instantiated on a different thread than the one you are on where you are trying to set it's 'Value' property. In that case you would just have to use the progressbars 'Invoke' method to call a delegate which will be executed on the same thread as the progress bar. If this is not what you are asking for then, try to be a little more specific as to what you want to know.




Re: Visual C# Language Threading/scope question, passing values from thread A to thread B

Kyle12

ScubaSteve20001 wrote:
I'm not sure of your question. Why can't you just set the progress bars 'Value' property to e.PercentDone. Unless of course the progress bar was instantiated on a different thread than the one you are on where you are trying to set it's 'Value' property. In that case you would just have to use the progressbars 'Invoke' method to call a delegate which will be executed on the same thread as the progress bar. If this is not what you are asking for then, try to be a little more specific as to what you want to know.

It's the delegate bit...




Re: Visual C# Language Threading/scope question, passing values from thread A to thread B

ScubaSteve20001

Here is some code that I just threw together that should allow you to update the progress bars value from any thread:

Code Block

public partial class Form1 : Form
{
private delegate void UpdateProgressBarDel(int value);
private UpdateProgressBarDel _delUpdateProgressBar;


public Form1()
{
InitializeComponent();
_delUpdateProgressBar = new UpdateProgressBarDel(UpdateProgressBar);
}

private void DoUpdateProgressBar(int value)
{
if (progressBar1.InvokeRequired == true)
progressBar1.Invoke(_delUpdateProgressBar, new object[] { value });
else
UpdateProgressBar(value);


}

private void UpdateProgressBar(int value)
{
progressBar1.Value = value;
}
}



So all you would have to do is call the 'DoUpdateProgressBar' method from whereever you like to update the progress bars value.