Hi,
Thank you for your reply!
However how will you access the properties of those controls?
If your only read from the properties of a control, you can do it directly.
For example,
int value = progressBar1.Value;
If you want to change the values of properties of a control and you will do
this from within the thread that this control is created on, you can do it
directly. For example, you add a ProgressBar on a form and you can change
the Value of this ProgressBar control from another form within this
application:
form1.Controls["progressBar1"].Value = 100;
However, if you want to change the values of properties of a control and
you will do this from a thread other than the thread that this control is
created on, you need to call the Control.Invoke or Control.BeginInvoke
method to execute a delegate on the thread that owns the control's
underlying window handle; otherwise, this will raise the Cross-thread
operation exception. The following is a sample.
public partial class Form1 : Form
{
delegate void SetProgressBarValueDelegate(object value);
public void SetProgressBarValue(object value)
{
if (this.InvokeRequired)
{
this.BeginInvoke(new
SetProgressBarValueDelegate(SetProgressBarValue), new object[] { value });
}
else
{
this.progressBar1.Value = Convert.ToInt32(value);
}
}
}
Then call the SetProgressBarValue method of the Form1 to change the value
of the ProgressBar control safely.
For more information on how to make thread-safe calls to Windows Forms
controls, please refer to the following MSDN document:
http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support