<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Peter Duniho's C# blog - All Comments</title><link>http://msmvps.com/blogs/duniho/default.aspx</link><description>One man&amp;#39;s various musings in C#, occasionally useful to others.</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1827731</link><pubDate>Thu, 18 Apr 2013 21:31:24 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1827731</guid><dc:creator>Bill</dc:creator><description>&lt;p&gt;thanks&lt;/p&gt;
&lt;p&gt;I understand a little more now, but absolutely definitely my code works without crashing!!!&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1827731" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1764400</link><pubDate>Tue, 27 Apr 2010 16:37:05 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764400</guid><dc:creator>Peter</dc:creator><description>&lt;p&gt;I have not seen the behavior you describe. It&amp;#39;s possible that the Control.Invoke() method had a bug that was fixed between .NET 2.0 (the version in which you say that happens) and .NET 3.5 SP1 (the version in which I checked).&lt;/p&gt;
&lt;p&gt;And to be clear: such behavior would in fact be a bug. Control.Invoke() should not fail silently. It either should invoke the delegate, or it should throw an exception. Failing to invoke the delegate but not throwing an exception would be unacceptable behavior.&lt;/p&gt;
&lt;p&gt;Note that the situation described in the Form.Close() method docs is when the MDI child has already been hidden and you call Close(). So I suppose it&amp;#39;s possible that the bug was intentional in a previous version of .NET. But in the versions I&amp;#39;m testing with, even closing the MDI child when it&amp;#39;s been hidden first doesn&amp;#39;t change the behavior: the form is still disposed, and attempting to invoke a delegate after the form has been disposed causes an exception to be thrown.&lt;/p&gt;
&lt;p&gt;If you have a concise code example that demonstrates the behavior you&amp;#39;re describing, especially if you can confirm that the behavior still exists in .NET 3.5 SP1 or later, please feel free to post it here.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1764400" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1764382</link><pubDate>Tue, 27 Apr 2010 13:56:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764382</guid><dc:creator>A J</dc:creator><description>&lt;p&gt;Sorry, I wrote that comment off in a hurry late last night. I will try to be clearer with stating my observations.&lt;/p&gt;
&lt;p&gt;However, at a basic level, we seem to be in agreement that there is more going on than a direct call to the delegate method when Invoke() is called from the UI thread that owns the control.&lt;/p&gt;
&lt;p&gt;I would also like to add that my observations were made with the .NET 2.0 framework.&lt;/p&gt;
&lt;p&gt;MDI child windows don&amp;#39;t behave the same way as normal forms when it comes to closing them (for the purpose of this discussion, I am not considering dialog windows, i.e. ShowDialog()). As can be seen from the documentation here (&lt;a rel="nofollow" target="_new" href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close.aspx"&gt;msdn.microsoft.com/.../system.windows.forms.form.close.aspx&lt;/a&gt;) closing a normal form ends up disposing it as well, whereas closing an MDI child form does not dispose it directly always.&lt;/p&gt;
&lt;p&gt;When I call Invoke() from a background thread and the target is an MDI child window (i.e. Invoke() is called over an MDI child form) and the MDI child window has not been closed yet, the delegate call is marshaled into the UI thread of the MDI child window (as should be).&lt;/p&gt;
&lt;p&gt;If the MDI child form is already closed by the time Invoke() is called from the background thread, the invocation fails with InvalidOperationException (as should be).&lt;/p&gt;
&lt;p&gt;When Invoke() is called from the UI thread of the MDI child form itself (i.e. in a situation where Invoke() is not required), the invocation behaves like a direct call to the delegate (again, as should be).&lt;/p&gt;
&lt;p&gt;However, when Invoke() is called from the UI thread of the MDI child form and the MDI child form has already been closed, the delegate method does NOT fire and an InvalidOperationException is NOT thrown. This is a result that I was not expecting - I was thinking (fairly reasonable thought, I think) that Invoke() would directly call the delegate method in this case. This is what made me conclude that there is more going on with Invoke() than just a direct call to the delegate method when Invoke() is not required.&lt;/p&gt;
&lt;p&gt;Hope that states my observations more clearly.&lt;/p&gt;
&lt;p&gt;Any thoughts on this?&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1764382" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1764372</link><pubDate>Tue, 27 Apr 2010 08:58:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764372</guid><dc:creator>Peter</dc:creator><description>&lt;p&gt;I am not sure what you&amp;#39;re trying to say.&lt;/p&gt;
&lt;p&gt;MDI child forms behave the same way that regular forms do: if not shown with the ShowDialog() method, then when the form is closed, first the FormClosed event is raised, during which the form has not yet been disposed and calls to Invoke() work normally, and then the Disposed event is raised, as the form is actually disposed, after which time calls to Invoke() would fail as would most other attempts to access the members of the form instance.&lt;/p&gt;
&lt;p&gt;It is not correct that closing an MDI child form does not dispose it, nor is it correct that if you call Invoke() before the form is disposed (in a handler of the FormClosed event, for example) that the invoked delegate is not executed.&lt;/p&gt;
&lt;p&gt;To confirm this, I tested using both .NET 3.5 and 4.0.&lt;/p&gt;
&lt;p&gt;Taken at it&amp;#39;s simplest, it is true that &amp;quot;there is more going on than directly calling the method when Invoke() is not required&amp;quot;, but only in the sense that the Invoke() method does do more than just call the method (a lot more, some of which is the equivalent of checking InvokeRequired).&lt;/p&gt;
&lt;p&gt;I am not aware of any scenario in which one could successfully execute code by checking InvokeRequired and skipping the call to Invoke() when the property returns false, but not by calling Invoke() directly. In particular, if calling Invoke() would fail, it&amp;#39;s because the form has been disposed and the call would throw an exception, in which case whatever code might have ever needed the call to Invoke() in any situation would also throw an exception (because the things in a form that require Invoke() are also the things that don&amp;#39;t work if the form has been disposed).&lt;/p&gt;
&lt;p&gt;If such a scenario does exist, closing an MDI child isn&amp;#39;t it. Calling Invoke() before the form is actually disposed works fine (e.g. in the FormClosed event handler), and attempting to call it after simply fails as one would expect (an InvalidOperationException is thrown).&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1764372" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1764363</link><pubDate>Tue, 27 Apr 2010 04:47:43 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1764363</guid><dc:creator>A J</dc:creator><description>&lt;p&gt;Not sure calling Invoke() when it is not required directly calls the method.&lt;/p&gt;
&lt;p&gt;For example, after closing an MDI child form (closing an MDI child form does not dispose it) if you call Invoke() over the form, the delegate method is not called (a breakpoint set there does not get hit). If Invoke() were called before the MDI child form was closed, the delegate method does get called (breakpoint would get hit).&lt;/p&gt;
&lt;p&gt;There is more going on than directly calling the method when Invoke() is not required.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1764363" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1762658</link><pubDate>Thu, 01 Apr 2010 03:47:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1762658</guid><dc:creator>Peter</dc:creator><description>&lt;p&gt;That&amp;#39;s true. But not really relevant to the article.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1762658" width="1" height="1"&gt;</description></item><item><title>re: MSDN's canonical technique for using Control.Invoke is lame</title><link>http://msmvps.com/blogs/duniho/archive/2008/09/12/msdn-s-canonical-technique-for-using-control-invoke-is-lame.aspx#1751844</link><pubDate>Fri, 15 Jan 2010 16:31:19 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1751844</guid><dc:creator>James H. Hansen</dc:creator><description>&lt;p&gt;The Invoke method cannot be called until the window handle is created. &amp;nbsp;Hence it will fail when initializing the text in a control before displaying the window.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1751844" width="1" height="1"&gt;</description></item></channel></rss>