{"id":1430,"date":"2010-12-25T09:55:21","date_gmt":"2010-12-25T04:25:21","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=1430"},"modified":"2010-12-25T09:55:21","modified_gmt":"2010-12-25T04:25:21","slug":"cross-thread-operation-not-valid-error-solved-in-c","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/cross-thread-operation-not-valid-error-solved-in-c\/","title":{"rendered":"Cross-thread operation not valid &#8211; error solved in C#"},"content":{"rendered":"<p>For the\u00a0beginners, working on thread might come across the error saying &#8220;<strong>Cross-thread operation not valid<\/strong>&#8220;. This type error comes normally when Windows control created in one thread and accessed in another.<br \/>\nBecause the Windows control are not thread safe by default.<\/p>\n<figure id=\"attachment_1432\" aria-describedby=\"caption-attachment-1432\" style=\"width: 352px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/12\/ThreadCommunication.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1432 \" title=\"ThreadCommunication\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/12\/ThreadCommunication.png?resize=352%2C137&#038;ssl=1\" alt=\"ThreadCommunication\" width=\"352\" height=\"137\" \/><\/a><figcaption id=\"caption-attachment-1432\" class=\"wp-caption-text\">Thread Communication<\/figcaption><\/figure>\n<p><!--more--><\/p>\n<p>The best way to solve this kind of error is :<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nif (InvokeRequired)\n                {\n                    this.Invoke(new MethodInvoker(delegate\n                    {\n                       \/\/Your code here, like set text box content or get text box contents etc..\n                    }));\n                }\n                else\n                {\n                     \/\/ Your code here, like set text box content or get text box contents etc..\n                     \/\/ SAME CODE AS ABOVE\n                }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cross-thread operation not valid &#8211; error solved in C#<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[22,17],"tags":[51],"class_list":["post-1430","post","type-post","status-publish","format-standard","hentry","category-csharp","category-tips","tag-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":414,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/background-and-foreground-thread-in-c\/","url_meta":{"origin":1430,"position":0},"title":"Background and foreground thread in c#","author":"Jitendra","date":"June 29, 2010","format":false,"excerpt":"Example of Threading in C#. Includes Foreground and Background Threading.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"Thread Foreground Background","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/ThreadDemo-300x93.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1029,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/logging-made-easy-in-net-log-4-net\/","url_meta":{"origin":1430,"position":1},"title":"Logging made easy in .NET &#8211; log 4 net","author":"Jitendra","date":"September 30, 2010","format":false,"excerpt":"How to use the log 4 net in .Net application. Example, demonstration and sourcecode","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":748,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/producer-consumer-problem\/","url_meta":{"origin":1430,"position":2},"title":"Thread Synchronization","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Describes Synchronization, Producer and Consumer Problem, wait(), notify(), InterThread Communication","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1820,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-threading-executor-framework-and-callable-interface\/","url_meta":{"origin":1430,"position":3},"title":"Java Threading &#8211; Executor Framework and Callable Interface","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Java Threading - Executor Framework and Callable Interface with example","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":724,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/simple-threading-techniques\/","url_meta":{"origin":1430,"position":4},"title":"Thread, Life Cycle of Thread","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"What is Thread? Life cycle of thread. Difference in start() and run() method of Thread. By Default how many thread runs?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1816,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/daemon-thread-java\/","url_meta":{"origin":1430,"position":5},"title":"Daemon Thread","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Example and Explanation of Daemon thread in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1430","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=1430"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/1430\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=1430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=1430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=1430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}