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
rkbnair - 24 Jan 2008 20:19 GMT
Hi Linda,
Thanks for the info.
The given code works fine. But feel hard to understand. Do you have any more
documentation regarding this usage?
Do you mean the following usage will create thread problems?
System.Windows.Forms.ProgressBar prgrs =
(System.Windows.Forms.ProgressBar)frm.Controls["progressBar1"];
prgrs.Maximum = int_progressbar_max;
prgrs.Value = 0;
> Hi,
>
[quoted text clipped - 50 lines]
> Linda Liu
> Microsoft Online Community Support
rkbnair - 24 Jan 2008 22:41 GMT
Hello Linda,
Could you explain what you mean by multiple threads? Is it possible to
explain it with an example please. My calling program is processing some
records of a dataset. Each record, it should do some operations. So, I want
to display a wait window with progressbar in it. Will this be a multithread?
Or how it can be a multithread application?
Thanks.
> Hi,
>
[quoted text clipped - 50 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 25 Jan 2008 09:21 GMT
Hi,
Thank you for your reply! I'm sorry that I didn't make myself clear in my
previous reply.
If both the calling form and the wait form are in one application, and you
don't create multiple threads by creating Thread objects explicitly or
calling the BeginInvoke method on a delegate, there's no multiple threads
in your application and you can access the ProgressBar instance on the wait
form from the calling form directly.
For more information on Thread class and asynchronous programming using
Delegates, please refer to the following MSDN documents:
'Thread Class'
http://msdn2.microsoft.com/en-us/library/system.threading.thread.aspx
'Asynchronous Programming Using Delegates'
http://msdn2.microsoft.com/en-us/library/22t547yb.aspx
The reason why I mentioned multiple threads in my previous reply is that I
noticed you said "frm (it contains the progressbar) is a form that is
called from another
program." in your first message. If the calling form and the wait form in
your practice belong to two running applications respectively, multiple
threads issue is related because the calling form and the wait form are
running on different threads.
Hope I make myself clear now.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support