Search The Blog
About this site

@RoyOsherove

Subscribe!

This site aims to connect all the dots of my online activities - from tools, books blogs and twitter accounts, to upcoming conferences, engagements and user group talks.

from 5whys.com
Twitter: @RoyOsherove
My Book: The Art of Unit Testing
Latest Posts
« I'm In! | Main | Ingo Rocks Too »
Tuesday
May202003

UI Threading - A Very Simple Solution

God loves the simple solutions. It turns out that there is a really simple solution to the problem of working with UI threads.Much simpler then mine. I was given this simple pattern by a guy on the win_tech_off_topic list. Here it is:

"I've read your article, and there is a simple pattern for multi-threaded

UI: in your event, you check the InvokeRequired parameter.

Eg: (this hasn't been anywhere near a compiler)

protected void windowsform_Event(object sender, EventArgs e)

{

if (this.InvokeRequired)

{

this.Invoke(new EventHandler(windowsform_Event), new object[] { sender, e });

return;

}

// do your magic here, and it will always be on the UI thread

}

Best wishes, James Berry"

 

Good one! I've been reading about UI threading in one of the past MSDN issues. The InvokeRequired Property returns true if the thread that reads it is not the UI thread of the control. Simple, elegant. What more could one ask for?

PrintView Printer Friendly Version

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>