{"id":387,"date":"2011-10-16T12:10:20","date_gmt":"2011-10-16T12:10:20","guid":{"rendered":"http:\/\/devnot.wordpress.com\/?p=387"},"modified":"2011-10-16T12:10:20","modified_gmt":"2011-10-16T12:10:20","slug":"invoke","status":"publish","type":"post","link":"https:\/\/thomas-jansen.eu\/?p=387","title":{"rendered":"Calling a form control from a (worker)thread."},"content":{"rendered":"<p>When using multiple threads controls on a form cannot be called directly. Doing so will throw an exception like &#8216;Cross-thread operation not valid: Control &#8216;dc&#8217; accessed from a thread other than the thread it was created on.&#8217;<\/p>\n<p>Fortunately there is a rather simple solution for this problem. Use the <em>Invoke<\/em> method to execute code in the GUI thread.<\/p>\n<p>The <em>invoke<\/em>method has the following prototype<\/p>\n<pre>public void Invoke(System.Delegate delegate);<\/pre>\n<p>For example: from a worker thread a button on a form (Form1)\u00a0 must be pressed. From inside the thread use the following construct to perform it thread safe.<\/p>\n<pre>Invoke((MethodInvoker)delegate\n{\n   Button1.Click();\n});<\/pre>\n<p><em>Invoke<\/em> is a member of the <em>Control<\/em> class. To make it work use a control created in the same thread.\u00a0 In the example above you can use Form1.Invoke() or Button1.Invoke() since they both are created in the same thread.<\/p>\n<p><em>MethodInvoker<\/em> is a default delegate definition. Its definition is\u00a0 delegate void MethodInvoker;<\/p>\n<p><strong>External delegates:<\/strong><\/p>\n<p>Of course the delegate can be external as well, sometimes this is favorable to get cleaner code or if the delegate is frequently used.<\/p>\n<pre>delegate void ClickTheButton();\nvoid clickTheButton()\n{\n   ...\n}<\/pre>\n<p>Inside the method<\/p>\n<pre>ClickTheButton clickTheButtonDelegate = clickTheButton;\nInvoke(clickTheButtonDelegate);<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Delegates with parameters<\/strong><\/p>\n<p>Delegates can also be called with parameters . This uses the second form of <em>Invoke()<\/em><\/p>\n<pre>public void Invoke(System.Delegate delegate, object [] args);\n\nvoid clickTheButton(int value)\n{\n   ...\n}\n\ndelegate void ClickTheButton(int);<\/pre>\n<p>Inside the method<\/p>\n<pre>\u00a0ClickTheButton clickTheButtonDelegate = clickTheButton;\n Invoke(clickTheButtonDelegate, new object[] {1});<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>When using multiple threads controls on a form cannot be called directly. Doing so will throw an exception like &#8216;Cross-thread operation not valid: Control &#8216;dc&#8217; accessed from a thread other than the thread it was created on.&#8217; Fortunately there is a rather simple solution for this problem. Use the Invoke method to execute code in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":0,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"wp:attachment":[{"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thomas-jansen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}