{"id":134,"date":"2010-03-19T18:34:56","date_gmt":"2010-03-19T18:34:56","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=134"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"thread-safe-calls","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/threads\/thread-safe-calls","title":{"rendered":"Thread Safe Calls"},"content":{"rendered":"<p>\nIf your application is doing asynchronous things, for instance a form has a callback function from a class receiving say some communications, then you will need to deal with calls to it in a thread safe way. The reason is that forms run on a single thread, but events in the other class will not necessarily be on the same thread. So to deal with this&#8230;\n<\/p>\n<h4>\nWhen you don&#39;t need to pass any arguments<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/DEAL WITH IN THREAD SAFE WAY\r\n\tvoid HandleEvent_MultiThreaded(void)\r\n\t{\r\n\t\t\/\/WE MAY NOT BE ON THE SAME THREAD AS THIS ACTUAL FORM, SO HANDLE CORRECTLY\r\n\t\tif (this-&gt;txtConnectionStatus-&gt;InvokeRequired)\t\t\t\t\t\t\t\/\/Change txtConnectionStatus to be some object on your form (or just use this-&gt;InvokeRequired if its possible)\r\n\t\t\tthis-&gt;Invoke(gcnew MethodInvoker(this, &amp;frmMain::HandleEvent));\t\t\/\/Change frmMain if necessary to your forms class\r\n\t\telse\r\n\t\t\tHandleEvent();\r\n\t}\r\n\r\n\tprivate: void HandleEvent (void)\r\n\t{\r\n\t\ttxtConnectionStatus-&gt;Text = &quot;Hello&quot;;\r\n\t\t\/\/...\r\n\t}\r\n<\/code><\/pre>\n<h4>\nWhen you do need to pass arguments<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\t\/\/Declare a delegate\r\n\tprivate: delegate void HandleEvent_Del (int Type, array&lt;unsigned char&gt; ^RxData, int RxLength);\r\n\r\n\t\/\/DEAL WITH IN THREAD SAFE WAY\r\n\tvoid HandleEvent_MultiThreaded(int Type, array&lt;unsigned char&gt; ^RxData, int RxLength)\r\n\t{\r\n\t\t\/\/WE MAY NOT BE ON THE SAME THREAD AS THIS ACTUAL FORM, SO HANDLE CORRECTLY\r\n\t\tif (this-&gt;txtConnectionStatus-&gt;InvokeRequired)\t\t\t\t\t\t\/\/Change txtConnectionStatus to be some object on your form (or just use this-&gt;InvokeRequired if its possible)\r\n\t\t\tthis-&gt;Invoke(gcnew HandleEvent_Del(this, &amp;frmMain::HandleEvent),Type, RxData, RxLength);\t\/\/Change frmMain if necessary to your forms class\r\n\t\telse\r\n\t\t\tHandleEvent(Type, RxData, RxLength);\r\n\t}\r\n\r\n\tprivate: void HandleEvent (int Type, array ^RxData, int RxLength)\r\n\t{\r\n\t\ttxtConnectionStatus-&gt;Text = Convert::ToString(Type);\r\n\t\t\/\/...\r\n\t}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If your application is doing asynchronous things, for instance a form has a callback function from a class receiving say some communications, then you will need to deal with calls to it in a thread safe way. The reason is that forms run on a single thread, but events in the other class will not [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":7,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":1137,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/134\/revisions\/1137"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}