<?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 Ritchie's MVP Blog : WinForms</title><link>http://msmvps.com/blogs/peterritchie/archive/tags/WinForms/default.aspx</link><description>Tags: WinForms</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>"Object is currently in use elsewhere" error.</title><link>http://msmvps.com/blogs/peterritchie/archive/2008/01/28/quot-object-is-currently-in-use-elsewhere-quot-error.aspx</link><pubDate>Mon, 28 Jan 2008 17:04:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1484207</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>11</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=1484207</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2008/01/28/quot-object-is-currently-in-use-elsewhere-quot-error.aspx#comments</comments><description>&lt;p&gt;I was debugging what I thought was a strange exception the other day.&amp;nbsp; The exception was an &lt;font face="courier new,courier"&gt;InvalidOperationException&lt;/font&gt; and the message was &amp;quot;Object is currently in use elsewhere&amp;quot;.&amp;nbsp; Unless you&amp;#39;re familiar with this exception, it really doesn&amp;#39;t offer much as to why the exception is occurring.&amp;nbsp; There seems to be several stale threads on the Web about this issue, so I&amp;#39;d thought I&amp;#39;d post about it.&lt;/p&gt;
&lt;p&gt;As it turns out it had to do with some code that was PInvoking some native graphics functions and the interaction with the WinForm that was hosting the drawing surface was to blame.&lt;/p&gt;
&lt;p&gt;What&amp;#39;s really happening with &amp;quot;Object is currently in use elsewhere&amp;quot; is that GDI+ is complaining that the device context (DC)&amp;nbsp;that it is trying to use is already &amp;quot;in use&amp;quot;.&amp;nbsp; With WinForms, this generally means there is a recursive &lt;font face="courier new,courier"&gt;Graphics.GetHdc&lt;/font&gt; occurring.&amp;nbsp; &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt; must match a &lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt; before any other &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;.&amp;nbsp; Recursive means you have something like &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;-&amp;gt;ReleaseHdc, instead of &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;.&amp;nbsp; Another possibility is that there is a missing call to&amp;nbsp;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;. (i.e. &lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;GetHdc&lt;/font&gt;-&amp;gt;&lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;)&lt;/p&gt;
&lt;p&gt;Now, in my case there was some seemingly innocuous code like this:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;SafeNativeMethods&lt;/span&gt;.DrawSomeStuff(e.Graphics.GetHdc(), parameters);&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.DrawString(text, &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.Font, &lt;span style="COLOR:#2b91af;"&gt;Brushes&lt;/span&gt;.Black, point);&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;...no matching &lt;font face="courier new,courier"&gt;ReleaseHdc&lt;/font&gt;(), before the &lt;font face="courier new,courier"&gt;DrawString&lt;/font&gt; call.&lt;/p&gt;
&lt;p&gt;The fix turned out to be really simple:&lt;/p&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;div style="FONT-SIZE:10pt;BACKGROUND:white;COLOR:black;FONT-FAMILY:Courier New;"&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;try&lt;/span&gt;&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:#2b91af;"&gt;SafeNativeMethods&lt;/span&gt;.DrawSomeStuff(e.Graphics.GetHdc(), parameters);&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="COLOR:blue;"&gt;finally&lt;/span&gt; &lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.ReleaseHdc();&lt;/p&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/p&gt;&lt;/div&gt;
&lt;p style="MARGIN:0px;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; e.Graphics.DrawString(text, &lt;span style="COLOR:blue;"&gt;this&lt;/span&gt;.Font, &lt;span style="COLOR:#2b91af;"&gt;Brushes&lt;/span&gt;.Black, point);&lt;/p&gt;&lt;/div&gt;
&lt;p&gt;You can also encounter this exception if you&amp;#39;re drawing to a form from multiple threads.&amp;nbsp; You&amp;#39;ll likely also be encountering a cross-threading exception as well.&amp;nbsp; The solution in this case is to not use multiple threads when accessing a form, including drawing.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1484207" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/DevCenterPost/default.aspx">DevCenterPost</category></item><item><title>Changing TextBox Text as an Undo-able Action</title><link>http://msmvps.com/blogs/peterritchie/archive/2006/09/10/Changing-TextBox-Text-as-an-Undo_2D00_able-Action.aspx</link><pubDate>Mon, 11 Sep 2006 01:23:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:121762</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=121762</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2006/09/10/Changing-TextBox-Text-as-an-Undo_2D00_able-Action.aspx#comments</comments><description>&lt;P&gt;The &lt;CODE&gt;TextBox&lt;/CODE&gt; class supports undoing the last action--inherited from &lt;CODE&gt;TextBoxBase&lt;/CODE&gt;.&amp;nbsp; Normally the user does this by pressing the undo key (Ctrl-Z if your keyboard doesn't have a specific Undo key) or by selecting "Undo" from the context menu.&amp;nbsp; The last action can also be undone programmatically by calling &lt;CODE&gt;TextBoxBase.Undo()&lt;/CODE&gt; (after calling &lt;CODE&gt;CanUndo()&lt;/CODE&gt; to see if &lt;CODE&gt;Undo()&lt;/CODE&gt; will work).&lt;/P&gt;
&lt;P&gt;Changing the text in a &lt;CODE&gt;TextBox&lt;/CODE&gt; so that the change can be undone is not so obvious though.&amp;nbsp; Changing the &lt;CODE&gt;Text&lt;/CODE&gt; property or the &lt;CODE&gt;SelectedText&lt;/CODE&gt; property is not undo-able.&amp;nbsp; .NET 2.0 added &lt;CODE&gt;TextBox.Paste(String)&lt;/CODE&gt; (not inherited from &lt;CODE&gt;TextBoxBase&lt;/CODE&gt;, it's inherent to &lt;CODE&gt;TextBox&lt;/CODE&gt;) that replaces the currently selected text and is undoable.&amp;nbsp; If you want to replace all the text while allowing the user to undo it, simply call &lt;CODE&gt;TextBoxBase.SelectAll()&lt;/CODE&gt; before &lt;CODE&gt;Paste(Sting)&lt;/CODE&gt;.&amp;nbsp; For example:&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT:silver 1px solid;PADDING-RIGHT:2px;BORDER-TOP:silver 1px solid;PADDING-LEFT:2px;FONT-SIZE:10pt;BACKGROUND:cornsilk;PADDING-BOTTOM:2px;MARGIN:2px;BORDER-LEFT:silver 1px solid;COLOR:black;PADDING-TOP:2px;BORDER-BOTTOM:silver 1px solid;FONT-FAMILY:Courier New;"&gt;
&lt;P style="MARGIN:0px;FONT-FAMILY:Courier New;"&gt;textBox.SelectAll();&lt;/P&gt;
&lt;P style="MARGIN:0px;FONT-FAMILY:Courier New;"&gt;textBox.Paste(text);&lt;/P&gt;&lt;/DIV&gt;
&lt;P&gt;This side-effect, unfortunately, is not documented--which is why it "is not so obvious".&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=121762" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+2.0/default.aspx">.NET 2.0</category></item><item><title>Docking With Run-Time-Created Controls</title><link>http://msmvps.com/blogs/peterritchie/archive/2006/07/07/RuntimeDockFill.aspx</link><pubDate>Fri, 07 Jul 2006 13:33:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:104125</guid><dc:creator>PeterRitchie</dc:creator><slash:comments>7</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/peterritchie/rsscomments.aspx?PostID=104125</wfw:commentRss><comments>http://msmvps.com/blogs/peterritchie/archive/2006/07/07/RuntimeDockFill.aspx#comments</comments><description>How to Dock Fill a control created at run-time....(&lt;a href="http://msmvps.com/blogs/peterritchie/archive/2006/07/07/RuntimeDockFill.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://msmvps.com/aggbug.aspx?PostID=104125" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/.NET+Development/default.aspx">.NET Development</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/WinForms/default.aspx">WinForms</category><category domain="http://msmvps.com/blogs/peterritchie/archive/tags/Software+Development/default.aspx">Software Development</category></item></channel></rss>