{"id":3987,"date":"2014-03-30T06:57:32","date_gmt":"2014-03-30T06:57:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/this-controls-add-error-system-invalideoperationexception-collection-of-common-programming-errors\/"},"modified":"2014-03-30T06:57:32","modified_gmt":"2014-03-30T06:57:32","slug":"this-controls-add-error-system-invalideoperationexception-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/this-controls-add-error-system-invalideoperationexception-collection-of-common-programming-errors\/","title":{"rendered":"this-&gt;Controls-&gt;Add ERROR System.InvalideOperationException-Collection of common programming errors"},"content":{"rendered":"<ul>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn \u00a0hello,I am developing code to add various controls to a windows form at runtime and I am recieving an error of &#8220;System.InvalidOperationException.&#8221;\u00a0 When\u00a0I\u00a0remove checking of illegal thread calls, I recieve and error of this &#8220;System.ArgumentException.&#8221;\u00a0 Location of Error is shown below.\u00a0<\/p>\n<p>I have this code also in the form load section and it executes fine.\u00a0 I then call this:<\/p>\n<p>ExecThreadDelegate* execThread =<\/p>\n<p>new ExecThreadDelegate(this,&amp;ProtoBRAT::StatusForm::ExecThread);execThread-&gt;BeginInvoke(0, 0);I am unable to add controls.\u00a0 Do i have to make another thread to add components?<\/p>\n<p>System::Collections::Generic::List* OpLabels;System::Windows::Forms::Label* temp; OpLabels = new System::Collections::Generic::List;<br \/>\ntemp = new Label();<\/p>\n<p>&#8230;&#8230;.<\/p>\n<p>&#8230;&#8230;.temp-&gt;Location = System::Drawing::Point(Xcoord, Ycoord);<\/p>\n<p>temp-&gt;Name = S<\/p>\n<p>&#8220;name&#8221;;temp-&gt;Size = System::Drawing::Size(150, 16);<\/p>\n<p>temp-&gt;Text = S<\/p>\n<p>&#8220;text&#8221;;<br \/>\nthis-&gt;Controls-&gt;Add(temp);\u00a0 \/\/ ERROR OCCURS HEREOpLabels-&gt;Add(temp);please let me know.<\/p>\n<p>thanks!<\/p>\n<\/li>\n<li><img decoding=\"async\" src=\"http:\/\/i.msdn.microsoft.com\/dn186180.LOGO_Win1211(id-id,MSDN.10).png\" \/><br \/>\nmsdn1 What you are doing is a serious mistake.\u00a0 Luckily, Windows itself caught it after you tried to shut up\u00a0the IllegalOperation exception.\u00a0 Windows is balking because you are creating a child control on a different thread than the parent control (the form).\u00a0 This is illegal in Windows.\u00a0 Before you set the CheckForIllegalCrossThreadCalls property to false, .NET was warning you that you were accessing a control from a thread that didn&#8217;t create it.This can cause serious and random failure of your program.\u00a0 It may run for hours or minutes.\u00a0 You may notice strange painting problems here or there.\u00a0 Sooner or later, the moon crosses in front of the sun and your program will deadlock solidly, requiring the three finger salute.<\/p>\n<p>The threading rules are simple: only ever create controls on the UI thread.\u00a0 Only ever access controls on the UI thread.\u00a0 If a background thread needs to update a control, it must use Control.Invoke() or BeginInvoke().\u00a0 Be sure to not mess with CheckForIllegalCrossThreadCalls, it warns you when you break the rules, even if by accident.<\/p>\n<p>Hans Passant.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>msdn \u00a0hello,I am developing code to add various controls to a windows form at runtime and I am recieving an error of &#8220;System.InvalidOperationException.&#8221;\u00a0 When\u00a0I\u00a0remove checking of illegal thread calls, I recieve and error of this &#8220;System.ArgumentException.&#8221;\u00a0 Location of Error is shown below.\u00a0 I have this code also in the form load section and it executes [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3987","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3987"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3987\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}