How can you delegate a function with parameters to an other thread in C#?
If I try it on my own I get this error:
error CS0149: Method name expected
This is what I have now:
delegate void BarUpdateDelegate();
private void UpdateBar(int Value,int Maximum,ProgressBar Bar)
{
if (Bar.InvokeRequired)
{
BarUpdateDelegate Delegation = new BarUpdateDelegate(Value, Maximum, Bar); //error CS0149: Method name expected
Bar.Invoke(Delegation);
return;
}
else
{
Bar.Maximum = Maximum;
Bar.Value = Value;
//Insert the percentage
int Percent = (int)(((double)Value / (double)Bar.Maximum) * 100);
Bar.CreateGraphics().DrawString(Percent.ToString() + "%", new Font("Arial", (float)8.25, FontStyle.Regular), Brushes.Black, new PointF(Bar.Width / 2 - 10, Bar.Height / 2 - 7));
return;
}
}
I want to update from an other thread the progress bar in the main thread.
You don’t initialize a delegate with arguments:
Instead, pass those arguments to
Invoke.You’ll also need to specify those arguments in your delegate definition. However, there is an easier way, using the built-in
Action<...>delegates. I also made a couple other code improvements.