Thursday, April 30, 2009

Set ProgressBar properties in a Thread

delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
private void SetControlPropertyValue(Control oControl, string propName, object propValue)
{
if (oControl.InvokeRequired)
{
SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
oControl.Invoke(d, new object[] { oControl, propName, propValue });
}
else
{
Type t = oControl.GetType();
PropertyInfo[] props = t.GetProperties();
foreach (PropertyInfo p in props)
{
if (p.Name.ToUpper() == propName.ToUpper())
{
p.SetValue(oControl, propValue, null);
}
}
}
}


Thread thread = new Thread(new ThreadStart(Generate));
thread.Name = "Generate";
thread.IsBackground = true;
thread.Start();


The control properties like progressbar properties can be set in Generate Method


SetControlPropertyValue(progressBar1, "Visible", true);
SetControlPropertyValue(progressBar1, "Minimum", 0);
SetControlPropertyValue(progressBar1, "Maximum", dtCompareSets.Rows.Count);

No comments: