Delegate puzzle

  • Thread starter Thread starter Chuck Bowling
  • Start date Start date
C

Chuck Bowling

I've got a problem. Not sure if it's a problem in the code or in my
understanding.

I have a class that contains a delegate. The delegate is assigned to a
simple method in a form that assigns the values of a series of strings to an
instance of the label control sequentially.

// ******
public void DisplayWord(FlashWord word)
{
lblFlash.Text = word.Text;
Debug.WriteLine("DisplayWord method - " + lblFlash.Text);
}
// ******

the method appears to work fine. Each time the delegate invokes DisplayWord
it assigns the value of word.Text to the label. The Debug.WriteLine method
shows that the label contains the text. However, the text never shows up in
the window - at least not until the last word in the sequence.

Anybody have a clue as to what's going on?
 
Hello

Your problem is not with delegates. Windows applications has a message
queue, and there is a loop that processes messages in first come first serve
fashion. When you change the Text for a label, the label control invalidates
itself, this places a WM_PAINT message in the queue. This message will not
get processed until the message loop finishes processing the current
message, which executes your method several times. When the message loop
processes the WM_PAINT, it sees only the last value.

If you want to have a label display a series of words sequentialy, consider
using System.Windows.Forms.Timer and update the label's text in the Tick
event handler with the next word.

Best regards,
Sherif
 
Thank you Sherif. I'll give that a try.

Sherif ElMetainy said:
Hello

Your problem is not with delegates. Windows applications has a message
queue, and there is a loop that processes messages in first come first serve
fashion. When you change the Text for a label, the label control invalidates
itself, this places a WM_PAINT message in the queue. This message will not
get processed until the message loop finishes processing the current
message, which executes your method several times. When the message loop
processes the WM_PAINT, it sees only the last value.

If you want to have a label display a series of words sequentialy, consider
using System.Windows.Forms.Timer and update the label's text in the Tick
event handler with the next word.

Best regards,
Sherif


to
 
Can I ask, what is the difference between a Forms.Timer and an EventHandler?

I tried using an instance of EventHandler to do the job and ran into a
similar problem.
 
Hello

An event Handler is a delagate type with the following signature
void EventHandler(object sender, EventArgs e);
It is used by many .NET classes to handle events such as Button.Click,
Timer.Tick, etc.

The Forms.Timer is a class than encapsulates a windows timer. The Timer
class has a Tick event which of EventHandler type. You can use this event so
that your method gets called whenever the Timer fires.

I am sorry for my bad english, but you will find in MSDN documentation a
much better explanation and examples.

Best regards,
Sherif
 
Thank you for the explanation.

So you that the only difference between the Forms.Timer EventHandler and the
other is in the arguments? That is something that I already knew. What I
don't understand is why using an event handler inside of a class had a
result very similar to my current problem. The class instance fired several
events but didn't update the label control until the method that
instantiated the class exited.
 
Back
Top