In one of my previous posts I discussed about the issues with using Dispatcher.Invoke. In this post, we are discussing some possible issues with Dispatcher.BeginInvoke and Closures. Let us start with an example and see the expected result. Then we will be discussing how we can improve the results.

Let us update the code behind with the definition of myButton_Click handler.

You probably are imagining the textbox to be updated as follows when we click the button. But as we click the button, the text box is updated as follow: Hmm! It's interesting, but why? Wanna know? Let us discuss!

Basically this is a known behavior of Captured variables. They are outer variables used by lambda expression / statement. These types of lambda expressions / statements are called Closures. As we all know there is delayed execution of lambda expression. While being executed, they use the current value of the captured variable rather than the value when they were created.

In order to solve this problem of closure, there is a universal remedy. Whenever we need to capture a variable in a closure, we must be making a local copy of the variable by declaring a local variable in lambda and assigning the value of captured variable to this local variable.

Let us update our code based on above explanation and see if that results in expected result. Let us update the handler for button click event as follows:

Now we run the application and click the button. The form updates as follows. As you can see this is fixed now as per expectation.
[null, {"document_url": "http://www.shujaat.net/2010/12/wpf-dispatcherbegininvoke-and-closures.html", "unformatted_src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TP707bKN0eI/AAAAAAAAAgU/ieGTT6Vo_mE/s400/1.jpg", "src": "http://3.bp.blogspot.com/_fVxKMZVpfzU/TP707bKN0eI/AAAAAAAAAgU/ieGTT6Vo_mE/s400/1.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-dispatcherbegininvoke-and-closures.html", "unformatted_src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TP707bsO2OI/AAAAAAAAAgc/7Czc4P60IHw/s400/2.jpg", "src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TP707bsO2OI/AAAAAAAAAgc/7Czc4P60IHw/s400/2.jpg"}, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-dispatcherbegininvoke-and-closures.html", "unformatted_src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TP707qPxyPI/AAAAAAAAAgk/GRF6s_FnZ_w/s400/3.jpg", "src": "http://4.bp.blogspot.com/_fVxKMZVpfzU/TP707qPxyPI/AAAAAAAAAgk/GRF6s_FnZ_w/s400/3.jpg"}, null, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-dispatcherbegininvoke-and-closures.html", "unformatted_src": "http://3.bp.blogspot.com/-StVialkfDd8/UCGhpO3j02I/AAAAAAAABuM/_XHqvYMcULs/s195/MVVMSuvivalGuideCover.png", "src": "http://3.bp.blogspot.com/-StVialkfDd8/UCGhpO3j02I/AAAAAAAABuM/_XHqvYMcULs/s195/MVVMSuvivalGuideCover.png", "formatted_filename": "MVVMSuvivalGuideCover", "rendered_width": 161, "rendered_height": 195}, null, null, null, null, {"document_url": "http://www.shujaat.net/2010/12/wpf-dispatcherbegininvoke-and-closures.html", "unformatted_src": "http://1.bp.blogspot.com/-AM-f-uCY_uU/UbDSwj9c0JI/AAAAAAAAF1Q/0iXTkrmoBZU/s195/VSM.png", "src": "http://1.bp.blogspot.com/-AM-f-uCY_uU/UbDSwj9c0JI/AAAAAAAAF1Q/0iXTkrmoBZU/s195/VSM.png", "formatted_filename": "VSM", "rendered_width": 195, "rendered_height": 54}, null, null, null, null, null]