Hey Mr. Skeet

  • Thread starter Thread starter Michael C
  • Start date Start date
M

Michael C

Something you said the other day, about hiding an asynchronous Invoke of a
UI control in a property, stuck with me. Can you point me to an example of
this? BTW - I'm about 1/4th of the way through your website. Wow! I'm
re-reading your article on Multi-threading in .NET. I was just trying to
find some information on the System.Threading.Timer and there you have it...
complete with examples! When is that book coming out???

Cheers,
Michael C
 
Michael C said:
Something you said the other day, about hiding an asynchronous Invoke of a
UI control in a property, stuck with me. Can you point me to an example of
this?

Sure. Here's a sample (untested, but should work):

delegate string GetString();
delegate void SetString(string value);

Label label; // Create instance elsewhere

public string LabelText
{
set
{
Invoke (new SetString(SetLabelText), new object[]{value});
}
get
{
return (string)Invoke (new GetString(GetLabelText));
}
}

void SetLabelText (string value)
{
label.Text = value;
}

string GetLabelText()
{
return label.Text;
}

Note that this isn't actually asynchronous - making the getter
asynchronous would be odd! - but it invokes it on the right thread. I
should probably put an example of this on the page at some stage. I've
got a whole bunch of things to add :)

One problem I've had with this is that if the form isn't visible when
the property is used, things go wrong. There's an inherent data race
which requires fairly careful handling - I'm not sure I've worked out
the best way of dealing with it yet. It's also an issue I haven't seen
mentioned much elsewhere...
BTW - I'm about 1/4th of the way through your website. Wow! I'm
re-reading your article on Multi-threading in .NET. I was just trying to
find some information on the System.Threading.Timer and there you have it...
complete with examples! When is that book coming out???

LOL - maybe it's time to start writing a proposal for a publisher. I
quite like the idea of a "Practical C#" book which is full of problems
people have actually run into...
 
Back
Top