Awesome Windows Forms message pump trick...

So WinForms does a great job of giving you a bunch of cool events to hook into your UI.  One of my favorites is quickly coming to be synchronizing the TreeView and ListBox classes.  So often you have some form of directory structure or hierarchical data that you need a TreeView for, and then some expanded version of that data to go over into the ListBox.  I've been blasting out a really cool picture viewer, so in my case, directories on the left, pictures on the right.

So I want a responsive UI.  I want my mouse wheel to immediately allow me to scroll the ListBox after I've populated it based on the tree view.  However, if you throw a Focus call into the AfterSelect, the ListBox gets focus, and then the TreeView calls Focus and get's its focus back.  This is kind of crappy, but there is a way to put your own messages on the message pump.  You see, BeginInvoke in Windows Forms is implemented using a special windows message.  Basically, when you BeginInvoke, a message is sent to your window, and when that message is received (on the UI thread none-the-less), it notifies the Control to call some delegate that you've specified.  Pretty cool huh?  I think so.  So why BeginInvoke and not just Invoke?  Well, Invoke I believe does the same thing.  However, it is synchronous, and so it executes inside of your current method (in our case the event handler for AfterSelect).  So again, the ListBox gets selected and then after our event handler exits, the TreeView does a Focus call again.  What we need is a way for the TreeView to call it's focus and then have our own Focus method be called.  Taking advantage of the message pump is just the way, because all of our code is going to execute, and then later on down the road when some time is freed up, the message pump will kick in and process our request.  To do this we'll need the appropriate delegates.  WinForms already defines a MethodInvoker, that does a void return and takes no arguments.  We'll create a new delegate, BoolMethodInvoker that returns a boolean instead.

private delegate bool BoolMethodInvoker();

Easy enough.  Now, remember, don't call Invoke on it, since that'll run the message pump synchronously.  We want to wait until after the tree has given itself focus for ours to run, so simply add the message to the pump and then return to our program execution.

listPictures.BeginInvoke(new BoolMethodInvoker(listPictures.Focus));

I've used this method countless times and it has only hit me as of late that it might be useful to others.  I remember getting around some weird issues in the GDN Workspaces WinForms control using just this code, so it was definitely useful there.  I highly recommend defining invoker delegates for all the method types you might want to call this way.  Just remember, that when using this, you never know when the method is actually going to execute.  You simply need to wait for the message pump to come around and service your message before your delegate is going to be called.

Published Friday, February 20, 2004 2:07 AM by Justin Rogers
Filed under: ,

Comments

Friday, February 20, 2004 12:56 PM by gilad g

# re: Awesome Windows Forms message pump trick...

...And with the power of anon. methods in C# Whidbey, there won't be any need for pre-defining a "Method Invoker" :)
Wednesday, June 02, 2004 6:03 AM by nani

# re: Awesome Windows Forms message pump trick...

nani
Wednesday, February 20, 2008 9:43 AM by ZoogeovEgoZop

# re: Awesome Windows Forms message pump trick...

Just discovered a complete list of all marked down products at Amazon, sorted by category

and % off, ranging from 50% off to 90% off (thanks Sonja for the effort).

Actually I never thought Amazon would have articles with 90% off, but only in the category

Electronics there are more than 3000 of them - look for yourself, the list is on

bargains-hunter.blogspot.com/.../looking-for-marked-down-prices.html

or on digg.com/.../Actually_I_never_thought_Amazon_would_have_articles_with_90

(which is a blog of a woman who specializes in finding good deals at Amazon, like Britain’s "Jeanie").

Thursday, November 12, 2009 10:31 AM by Chip Patton

# re: Awesome Windows Forms message pump trick...

Justin,

Exactly what I needed; next time I will include the word "Awesome" and "Trick" in my search: "Awesome Windows Forms Message Pump Trick".

For me, we were using a FormBorderStyle that is not directly supported by Windows Forms: sizable with no caption (we use our own window for caption stuff with bigger system menu buttons for touch screen use and add some tabs, too).  When Minimize or Maximize and then restore to Normal, Windows Forms tries to compensate for the Caption (that isn't there!) and the window grows.  We needed to queue up setting the size back where it belongs, none of the events are late enough Layout/Resize/SizeChanged (and ResizeEnd doesn't fire if the caption is turned off).

Thanks!

Wednesday, June 23, 2010 5:20 PM by jeff10d

# re: Awesome Windows Forms message pump trick...

Have you ever thought what it would be like to travel the states in your RV with

no worries and nothing holding you back! Follow me as I do just that! I live my life one mile at a time because I'm a Road Bandit!

http://roadbandit.com

Sunday, September 12, 2010 9:23 PM by Jenny Swift

# re: Awesome Windows Forms message pump trick...

It is extremely interesting for me to read this post. Thanx for it. I like such themes and anything connected to this matter. I definitely want to read a bit more on that blog soon. BTW, rather nice design you have here, but how about changing it once in a few months?

Jenny  Swift

<a href="www.baccaratgirls.com/">gfe escorts</a>

Monday, September 20, 2010 6:56 AM by John Kripke

# re: Awesome Windows Forms message pump trick...

Interesting story as for me. It would be great to read more about this matter. The only thing this blog needs is a few pictures of any gadgets.

John Kripke

<a href="www.jammer-store.com/">buy cell phone jammers</a>

Wednesday, November 17, 2010 8:38 PM by Kate Swift

# re: Awesome Windows Forms message pump trick...

Damn, truly interesting post. Where can I get your subscription?

Kate  Swift

<a href="www.wirelesscameradetectors.com/">spy shop</a>

Sunday, March 20, 2011 7:27 AM by Julia Smith

# re: Awesome Windows Forms message pump trick...

It was certainly interesting for me to read this article. Thanks the author for it. I like such topics and everything connected to them. I would like to read more soon.

Julia Smith    

<a href="milanescorts.com/">accompagnatrice escort girl a milano</a>

Sunday, April 17, 2011 4:05 PM by Haywoodqsere

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.innovatepa.org/fendihandbags.html> Fendi Handbags</a>

and found this post.  I would say that you have answered my question thanks

Saturday, April 23, 2011 11:16 AM by Colevserh

# re: Awesome Windows Forms message pump trick...

I was search for <a href=http://www.ezbuycomputers.com> ups </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, April 27, 2011 4:21 PM by Anete Simpson

# re: Awesome Windows Forms message pump trick...

Rather nice site you've got here. Thanx for it. I like such themes and anything that is connected to this matter. I definitely want to read more soon.  

Anete Simpson    

<a href="www.pickescort.com/">east london escorts</a>

Monday, May 02, 2011 8:47 PM by Kendraksere

# re: Awesome Windows Forms message pump trick...

If your looking for information on watches, check out <a href=http://www.citizenwristwatches.com>Citizen Watches</a>

Tuesday, May 03, 2011 4:49 AM by Kendranserm

# re: Awesome Windows Forms message pump trick...

If your looking for information on watches, check out <a href=http://www.citizenwristwatches.com>Citizen Watches</a>

Tuesday, May 03, 2011 12:34 PM by Averyxsere

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.lamps-lighting.com/search.php Feiss Wood</a>

and found this post.  I would say that you have answered my question thanks

Tuesday, May 03, 2011 10:35 PM by Saundraiserg

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.innovatepa.org/>Prada Handbags</a>

and found this post.  I would say that you have answered my question thanks

Thursday, May 05, 2011 1:39 PM by Augustushseru

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../DKT3001-REF.html>Toshiba DKT3001</a>

and found this post.  I would say that you have answered my question thanks

Friday, May 06, 2011 4:45 AM by Kendrakserh

# re: Awesome Windows Forms message pump trick...

I was looking for some information on <a href=www.gohobbies.com/plastic_models.html>Plastic Model Kits</a> I love hobbies!

Sunday, May 08, 2011 4:14 PM by Estelaeserq

# re: Awesome Windows Forms message pump trick...

I was looking for information on <a href=www.gohobbies.com/rc_cars-trucks.html>RC Cars</a> and its very helpfull. Anyone know any other sites?

Monday, May 09, 2011 6:12 PM by Jameykserv

# re: Awesome Windows Forms message pump trick...

<a href=www.gohobbies.com/rc_boats.html>Remote Control Boats</a> Are an awesome hobby. If you know anything about rc boats please let me know

Tuesday, May 10, 2011 5:16 AM by Jeneeksers

# re: Awesome Windows Forms message pump trick...

If you ever want to buy a <a href=www.gohobbies.com/rc_airplanes.html>RC Planes</a> That is a great spot. They have all hobbies.

Wednesday, May 11, 2011 4:07 AM by Nathanialqserr

# re: Awesome Windows Forms message pump trick...

Does anyone have a recomendation for a good <a href=www.gohobbies.com/rc_helicopters.html>Remote Control Helicopters</a>

Tuesday, May 17, 2011 10:30 PM by Taylorosert

# re: Awesome Windows Forms message pump trick...

If you are looking for any information on <a href=www.gohobbies.com/rc_boats.htm>lRemote Control Boats</a> look no further!

Friday, May 20, 2011 1:32 AM by Jarvisysers

# re: Awesome Windows Forms message pump trick...

If you are looking for any information on <a href=www.gohobbies.com/rc_airplanes.html>Remote Control Planes</a> i have found the best spot ever.

Friday, May 20, 2011 2:42 PM by Donovanbseri

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Saturday, May 21, 2011 12:42 AM by Gennyrsern

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Saturday, May 21, 2011 12:43 PM by Sherrilxserl

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Saturday, May 21, 2011 8:19 PM by Shanellepserw

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Sunday, May 22, 2011 11:53 AM by Garlandwsera

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Sunday, May 22, 2011 8:51 PM by Donovanwserk

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Monday, May 23, 2011 8:11 PM by Marlinzserf

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.keyphonesdirect.com/.../a>

and found this post.  I would say that you have answered my question thanks

Tuesday, May 24, 2011 6:49 AM by Myrtiswserz

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.lamps-lighting.com/.../20635-BAC.html> Designers Fountain Berkshire 20635-BAC </a>

and found this post.  I would say that you have answered my question thanks

Thursday, May 26, 2011 2:11 AM by Kirbyfserl

# re: Awesome Windows Forms message pump trick...

Looking for infoatmion on <a href=www.gohobbies.com/rc_boats.html>Remote Control Boats

</a> I found this great hobby site

Thursday, May 26, 2011 6:46 PM by Silassserk

# re: Awesome Windows Forms message pump trick...

Want some information on <a href=www.gohobbies.com/rc_airplanes.html>Remote Control Planes

</a> be sure to check these guys out.

Friday, May 27, 2011 10:18 PM by Kendrahserc

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../5012510.html> Gyros Precision Tool Inc 5012510 Safety Guard Kit </a>

and found this post.  I would say that you have answered my question thanks

Saturday, May 28, 2011 1:21 PM by Omarkserl

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../30769.html> OFNA Racing 30769 Gear, Bevel, Silver,4mm: RTR </a>

and found this post.  I would say that you have answered my question thanks

Sunday, May 29, 2011 4:05 AM by Dillonpserj

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../16007.html> Excell Hobby Products 16007 Knife </a>

and found this post.  I would say that you have answered my question thanks

Sunday, May 29, 2011 6:07 PM by Faustinolserp

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../3031.html> Hobbyzone 3031 Mini Mauler </a>

and found this post.  I would say that you have answered my question thanks

Monday, May 30, 2011 7:39 AM by Mirnazsere

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../611.html> Pegasus Hobby 611 Tank </a>

and found this post.  I would say that you have answered my question thanks

Monday, May 30, 2011 9:44 PM by gserx

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../A5301.html> Team Losi A5301 10mm Sedan Shock Set (4) </a>

and found this post.  I would say that you have answered my question thanks

Tuesday, May 31, 2011 11:12 AM by Randellgseri

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../A9154.html> Team Losi A9154 8IGHT B, B 2.0, T, T 2.0 </a>

and found this post.  I would say that you have answered my question thanks

Tuesday, May 31, 2011 6:47 PM by Corrinnemsero

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../125753.html> Hitec RCD 125753 Aggressor 2Ch,1-311,AM 75Mhz </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, June 01, 2011 12:19 AM by Franciscogserx

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../3038.html> Novak Electronics 3038 Havoc Sport 8.5TSensoredBLSystem </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, June 01, 2011 5:48 AM by Nathanialpsers

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../1732.html> Airfix Models 1732 Commandos </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, June 01, 2011 11:15 AM by Corrinnewserr

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../7055.html> E Flite 7055 F-15 Eagle DF </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, June 01, 2011 4:42 PM by mserq

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../67383.html> Hot Bodies RC 67383 D8, D8T </a>

and found this post.  I would say that you have answered my question thanks

Wednesday, June 01, 2011 10:04 PM by mserb

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../4037.html> Hangar 9 4037 Piper Pawnee 40 </a>

and found this post.  I would say that you have answered my question thanks

Thursday, June 02, 2011 3:37 AM by eserq

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../424.html> Dremel Tools 424 Mandrel </a>

and found this post.  I would say that you have answered my question thanks

Thursday, June 02, 2011 9:06 AM by Octaviohserr

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../7014.html> Sportwerks 7014 REA </a>

and found this post.  I would say that you have answered my question thanks

Friday, June 03, 2011 4:49 PM by Taylorosere

# re: Awesome Windows Forms message pump trick...

If you are looking for any information on <a href=www.gohobbies.com/rc_boats.htm>lRemote Control Boats</a> look no further!

Friday, June 03, 2011 10:39 PM by Jarviswserf

# re: Awesome Windows Forms message pump trick...

If you are looking for any information on <a href=www.gohobbies.com/rc_airplanes.html>Remote Control Airplanes</a> i have found the best spot ever.

Saturday, June 04, 2011 4:16 AM by Donovangserd

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../85529.html> HPI Racing 85529 Body Accessory Set, Black </a>

and found this post.  I would say that you have answered my question thanks

Sunday, June 05, 2011 1:44 PM by Gennyesert

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../86173.html> HPI Racing 86173 Alum Mnt/Retainer Set:SS </a>

and found this post.  I would say that you have answered my question thanks

Sunday, June 05, 2011 7:36 PM by Sherrilwsers

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../8029.html> Dremel Tools 8029 Blade </a>

and found this post.  I would say that you have answered my question thanks

Monday, June 06, 2011 1:42 AM by Shanelleasert

# re: Awesome Windows Forms message pump trick...

I was search for <a href=www.gohobbies.com/.../970582.html> JR Radios 970582 Vibe 500E </a>

and found this post.  I would say that you have answered my question thanks

Friday, June 17, 2011 7:45 PM by Strattera Rezeptfrei

# re: Awesome Windows Forms message pump trick...

Great minds think alike.

Sunday, January 01, 2012 7:00 PM by Moogedelilah

# re: Awesome Windows Forms message pump trick...

look at <a href=fakelvbagsuk.zoomshare.com/>fake louis vuitton bags uk</a>  with low price   to your friends

Monday, January 09, 2012 8:54 AM by Moogechristene

# re: Awesome Windows Forms message pump trick...

for <a href=dvd-to-apple-tv.weebly.com/>dvd to apple tv</a>  to your friends   suprisely

Monday, January 09, 2012 4:06 PM by Moogeannamarie

# re: Awesome Windows Forms message pump trick...

cheap <a href=www.gucciauthentic.com/>authentic gucci handbag</a>  for gift   , just clicks away

Friday, January 20, 2012 6:39 PM by biamstaren

# re: Awesome Windows Forms message pump trick...

you must read <a href=dvd-to-wii.weebly.com/>dvd to wii</a>  for more detail   , just clicks away

Leave a Comment

(required) 
(required) 
(optional)
(required)