newbie: stuck on trying to modify a control porerty from within created thread. Pls some help.

J

Juan

How can I call a method that modifies a control in the user interface from a
thread? When I tried to do it I receive an error stating that I shoul use
"Control.Invoke...".
I know I have to use a delegate

This is what I`m doing:

Create the delegate
1. public delegate void DelegateName(string[] tokens);

Create the event

2. public event DelegateName EventName;

Call the event (from with in the code tha is being executed in the thread I
created)

3. EventName(parameter)

I know i have to do something like

SomeObject.EventName+= new DelegateName(MethodHandlerName);

Every thing is coded inside the gui form's class. I know this should be
simple but I can not figure it out!
 
F

Fitim Skenderi

Hi Juan,

You need to call BeginInvoke method of the delegate from the thread.

Fitim Skenderi
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi Fitin,

It doesn't matter where the method trying to modify the control is declared
(in the control class or not) what is important is the thread executing that
code.

So assuming that you have an object that has some method executed in a
worker thread. That method fires an event whenever something happen. That
event is hooked by the control class. When the event is fired the event
handler (wich is method of the control class) is executed by the worker
thread not the UI thread (the thread created the control). You can change
the UI in the WindowsForms only from code which is currently executed by
that UI thread not simply from methods declared in the control class.

So what you have to do is to switch the execution in the UI thread. Whether
you have to switch the thread or not you can find out checking
Control.InvokeRequired property. This property check whether the code
reading it is running in the same thread as the code created the control. If
it returns 'true' the thread has to be switched. Control.Invoke and
Control.BeginInvoke (not to be confused with the Invoke and BeginInvoke
methods of the delegates which actully starts a new thread) are meant to be
used exactly for that.

So what is basically the pattern you might want to fallow.

Let say you have a method in the control class and you want to use that
method for changing the control's visual appearance (operations which are
thread sensitive). For simplicity I'm gonna use method without parameters,
but it goes the same for any method as long as you use proper delegate
instead of MethodInvoker.

void UpdateUI() //this method is memeber of class inheriting from Control
{
if(this.InvokeRequired)
{
this.Invoke(new MethodInvoker(UpdateUI));
return;
}
//Do the update here.
}

If you want the worker thread to wait until the control is done with the
update use Control.Invoke. If you don't want the worker thread to wait use
Control.BeginInvoke and the update is going to run concurrently with the
worker thread.

--
HTH
B\rgds
100 [C# MVP]

Fitim Skenderi said:
Hi Juan,

You need to call BeginInvoke method of the delegate from the thread.

Fitim Skenderi



Juan said:
How can I call a method that modifies a control in the user interface
from
a
thread? When I tried to do it I receive an error stating that I shoul use
"Control.Invoke...".
I know I have to use a delegate

This is what I`m doing:

Create the delegate
1. public delegate void DelegateName(string[] tokens);

Create the event

2. public event DelegateName EventName;

Call the event (from with in the code tha is being executed in the
thread
I
created)

3. EventName(parameter)

I know i have to do something like

SomeObject.EventName+= new DelegateName(MethodHandlerName);

Every thing is coded inside the gui form's class. I know this should be
simple but I can not figure it out!
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Sorry Fitim, I addressed my post wrongfully to you. Actually it is for Juan
:)


--
B\rgds
100 [C# MVP]
Fitim Skenderi said:
Hi Juan,

You need to call BeginInvoke method of the delegate from the thread.

Fitim Skenderi



Juan said:
How can I call a method that modifies a control in the user interface
from
a
thread? When I tried to do it I receive an error stating that I shoul use
"Control.Invoke...".
I know I have to use a delegate

This is what I`m doing:

Create the delegate
1. public delegate void DelegateName(string[] tokens);

Create the event

2. public event DelegateName EventName;

Call the event (from with in the code tha is being executed in the
thread
I
created)

3. EventName(parameter)

I know i have to do something like

SomeObject.EventName+= new DelegateName(MethodHandlerName);

Every thing is coded inside the gui form's class. I know this should be
simple but I can not figure it out!
 
J

Juan

excellent, thanks a lot for the help!
Juan.

Stoitcho Goutsev (100) said:
Sorry Fitim, I addressed my post wrongfully to you. Actually it is for Juan
:)


--
B\rgds
100 [C# MVP]
Fitim Skenderi said:
Hi Juan,

You need to call BeginInvoke method of the delegate from the thread.

Fitim Skenderi



Juan said:
How can I call a method that modifies a control in the user interface
from
a
thread? When I tried to do it I receive an error stating that I shoul use
"Control.Invoke...".
I know I have to use a delegate

This is what I`m doing:

Create the delegate
1. public delegate void DelegateName(string[] tokens);

Create the event

2. public event DelegateName EventName;

Call the event (from with in the code tha is being executed in the
thread
I
created)

3. EventName(parameter)

I know i have to do something like

SomeObject.EventName+= new DelegateName(MethodHandlerName);

Every thing is coded inside the gui form's class. I know this should be
simple but I can not figure it out!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top