{"id":387,"date":"2019-07-25T17:44:10","date_gmt":"2019-07-25T16:44:10","guid":{"rendered":"http:\/\/wp.bizoir.dk\/?p=387"},"modified":"2019-07-27T07:53:00","modified_gmt":"2019-07-27T06:53:00","slug":"net-threadsafe-updating-controls-from-a-non-gui-thread","status":"publish","type":"post","link":"https:\/\/wp.bizoir.dk\/?p=387","title":{"rendered":".NET &#8211; ThreadSafe updating controls from a non-gui thread"},"content":{"rendered":"<h3>Thread-safe updating of controls from another thread<\/h3>\n<p><a href=\"https:\/\/stackoverflow.com\/questions\/661561\/how-do-i-update-the-gui-from-another-thread\" rel=\"nofollow\">https:\/\/stackoverflow.com\/questions\/661561\/how-do-i-update-the-gui-from-another-thread<\/a><\/p>\n<h3>Another method (from BB)<\/h3>\n<pre>private void buttonOk_Click(object sender, EventArgs e)\r\n{\r\nCursor = Cursors.WaitCursor;\r\nbuttonOk.Enabled = false;\r\nint count = 0;\r\nSaveSettings();\r\nfor (int i = 0; i &lt; (int)NoOfMessages; i++)\r\n{\r\nnew Thread(new ThreadStart(() =&gt;\r\n{\r\nStopwatch watch = new Stopwatch();\r\nwatch.Start();\r\nvar message = PerformWork(.........);\r\nwatch.Stop();\r\nvar tempCount = ++count;\r\n\r\nInvoke(new MethodInvoker(() =&gt;\r\n{\r\ntextBoxLog.Text += \"Message \" + (tempCount) + \" done.\" + Environment.NewLine;\r\nif (tempCount == (int)NoOfMessages)\r\n{\r\nCursor = Cursors.Default;\r\nbuttonOk.Enabled = true;\r\n}\r\n\/\/ Console.WriteLine(\"DONE \" + tempCount);\r\n}));\r\n})).Start();\r\n}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Thread-safe updating of controls from another thread https:\/\/stackoverflow.com\/questions\/661561\/how-do-i-update-the-gui-from-another-thread Another method (from BB) private void buttonOk_Click(object sender, EventArgs e) { Cursor = Cursors.WaitCursor; buttonOk.Enabled = false; int count = 0; SaveSettings(); for (int i = 0; i &lt; (int)NoOfMessages; i++) { new Thread(new ThreadStart(() =&gt; { Stopwatch watch = new Stopwatch(); watch.Start(); var message = PerformWork(&#8230;&#8230;&#8230;); &hellip; <a href=\"https:\/\/wp.bizoir.dk\/?p=387\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;.NET &#8211; ThreadSafe updating controls from a non-gui thread&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[6],"class_list":["post-387","post","type-post","status-publish","format-standard","hentry","category-guides","tag-dotnet"],"_links":{"self":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/387","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=387"}],"version-history":[{"count":7,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions"}],"predecessor-version":[{"id":559,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=\/wp\/v2\/posts\/387\/revisions\/559"}],"wp:attachment":[{"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=387"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=387"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.bizoir.dk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=387"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}