{"id":4321,"date":"2014-03-30T09:51:29","date_gmt":"2014-03-30T09:51:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/i-need-some-help-using-threads-in-c-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:51:29","modified_gmt":"2014-03-30T09:51:29","slug":"i-need-some-help-using-threads-in-c-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/i-need-some-help-using-threads-in-c-collection-of-common-programming-errors\/","title":{"rendered":"I need some help using Threads in C#-Collection of common programming errors"},"content":{"rendered":"<p>I\u00b4m developing a software in C# that uses static functions from a C++ .dll file through a Wrapper.<\/p>\n<p>The problem is that some of these functions are slow and unstable, so in order to solve this I created a Thread that executes them. However, when I abort that thread from the main thread, the program isn\u00b4t letting me use those functions again, even though I define a new instance of the thread every time I call a function.<\/p>\n<p>Is there any way I can fix this?<\/p>\n<p>Thanks in advance.<\/p>\n<p>PS: Here is some code from my program:<\/p>\n<pre><code>public partial class MainForm : Form, IMultiLanguage\n{\n      \/\/...\n\n      \/\/This method is subscribed to the event of pressing the 'Abort' button\n      private void StopCurrentAnalisis()\n      {\n            try\n            {\n                this.analisisManagerController.AnalisisThread.Abort();\n            }\n            catch (Exception e){  }\n            finally\n            {\n                MessageBox.Show(\"Analisis has been cancelled by user\", \"Analisis Interrupted\", MessageBoxButtons.OK, MessageBoxIcon.Stop);\n                CerrarNowLoadingForm();\n            }\n      }\n\n      \/\/..\n}\n\npublic class AnalisisManager: IAnalisisManagerController\n{\n    \/\/..\n    private Thread analisisThread;\n    public Thread AnalisisThread{get{return this.analisisThread;}}\n\n\n    public void MakePadrobAnalisis(TipoAnalisis tipoAnalisis,\n        Dictionary parametros)\n    {\n        object[] arregloParams = new object[]{tipoAnalisis,parametros};\n        analisisThread = new Thread(new ParameterizedThreadStart(MakeAnalisisInOtherThread));\n        analisisThread.Start(arregloParams);\n    }\n\n private void MakeAnalisisInOtherThread(object o)\n    {\n        object[] arregloParams = o as object[];\n        TipoAnalisis tipoAnalisis = (TipoAnalisis) arregloParams[0];\n        Dictionary parametros = arregloParams[1] as Dictionary;\n\n        \/\/This launches an event telling the GUI the unstable analisis has started.\n        \/\/The method that makes the 'Abort' button to appear on the GUI is subscribed to this event\n        StartAnalisis();\n\n       \/\/The Thread executes DLL functions according to tipoAnalisis, for example:\n       case InvKinematicsMinTorque:\n       {\n           WrapperPadirob.InverseKinematicsMinTorqueConfigAnalisis();\n           break;\n       }\n       \/\/..\n   }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I\u00b4m developing a software in C# that uses static functions from a C++ .dll file through a Wrapper. The problem is that some of these functions are slow and unstable, so in order to solve this I created a Thread that executes them. However, when I abort that thread from the main thread, the program [&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-4321","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4321","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=4321"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4321\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}