{"id":18270,"date":"2021-09-01T17:52:45","date_gmt":"2021-09-01T16:52:45","guid":{"rendered":"https:\/\/developers.primaverabss.com\/en\/questions\/como-abrir-customform-usando-multithreading\/"},"modified":"2021-09-01T17:52:46","modified_gmt":"2021-09-01T16:52:46","slug":"como-abrir-customform-usando-multithreading","status":"publish","type":"cma_thread","link":"https:\/\/developers.ila.cegid.com\/en\/forum\/como-abrir-customform-usando-multithreading\/","title":{"rendered":"Como abrir CustomForm usando Multithreading"},"content":{"rendered":"<p>Objectivo: ao abrir uma empresa ter uma s\u00e9rie de c\u00f3digo a correr &#8220;por tras&#8221; de forma a n\u00e3o bloquear a UI e, quando eventualmente acabar, abrir um CustomForm se necess\u00e1rio.<\/p>\n<p>O seguinte c\u00f3digo &#8220;funciona&#8221; mas depois d\u00e1 erro na inicializa\u00e7\u00e3o do form, nomeadamente no componente PriGrelha.<\/p>\n<p>Presumivelmente tenho de o correr dentro de um BeginInvoke mas para isso preciso de passar a instancia do &#8220;main form&#8221; do ERP e n\u00e3o sei como o ir buscar dentro do evento DepoisDeAbrirEmpresa<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\">private System.ComponentModel.BackgroundWorker backgroundWorker1;  \r\n\r\npublic override void DepoisDeAbrirEmpresa(ExtensibilityEventArgs e)\r\n{\r\n\t\tthis.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();\r\n\t\tthis.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);\r\n\t\tthis.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);\r\n\r\n\t\t\/\/\/\/ Start BackgroundWorker\r\n\t\tbackgroundWorker1.RunWorkerAsync();\r\n}\r\n\r\nprivate void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)\r\n{\r\n\tBackgroundWorker helperBW = sender as BackgroundWorker;\r\n\t\/\/int arg = (int)e.Argument;\r\n\te.Result = BackgroundProcessLogicMethod(helperBW);\r\n\tif (helperBW.CancellationPending)\r\n\t{\r\n\t\te.Cancel = true;\r\n\t}\r\n}\r\n\/\/ Put all of background logic that is taking too much time      \r\nprivate int BackgroundProcessLogicMethod(BackgroundWorker bw)\r\n{\r\n\t\/\/very lengthy code here\r\n\r\n\treturn alertas;\r\n}\r\nprivate void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)\r\n{\r\n\r\n\tint alertas = PSO.Utils.FInt(e.Result);\r\n\r\n\tif (alertas &gt; 0)\r\n\t{\r\n\r\n\t\tPSO.PainelNotificacoes.MostraAlerta(\"Auditor UWU\", string.Format(\"Aten\u00e7\u00e3o, tem {0} alertas para validar!\", alertas));\r\n\r\n\t\tusing (var instance = this.BSO.Extensibility.CreateCustomFormInstance(typeof(Forms.CBL.AuditorCBL)))\r\n\t\t{\r\n\t\t\tif (instance.IsSuccess())\r\n\t\t\t{\r\n\t\t\t\t(instance.Result as Forms.CBL.AuditorCBL).Show();\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t}\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Objectivo: ao abrir uma empresa ter uma s\u00e9rie de c\u00f3digo a correr &#8220;por tras&#8221; de forma a n\u00e3o bloquear a UI e, quando eventualmente acabar, abrir um CustomForm se necess\u00e1rio. O seguinte c\u00f3digo &#8220;funciona&#8221; mas depois d\u00e1 erro na inicializa\u00e7\u00e3o do form, nomeadamente no componente PriGrelha. Presumivelmente tenho de o correr dentro de um BeginInvoke [&hellip;]<\/p>\n","protected":false},"author":1206,"featured_media":0,"template":"","tags":[],"cma_category":[],"cma_difficulty_level":[],"forum-tag":[370,374],"class_list":["post-18270","cma_thread","type-cma_thread","status-publish","hentry","forum-tag-integration","forum-tag-v10-en","forum-languages-pt"],"_links":{"self":[{"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/cma_thread\/18270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/cma_thread"}],"about":[{"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/types\/cma_thread"}],"author":[{"embeddable":true,"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/users\/1206"}],"wp:attachment":[{"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/media?parent=18270"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/tags?post=18270"},{"taxonomy":"cma_category","embeddable":true,"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/cma_category?post=18270"},{"taxonomy":"cma_difficulty_level","embeddable":true,"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/cma_difficulty_level?post=18270"},{"taxonomy":"forum-tag","embeddable":true,"href":"https:\/\/developers.ila.cegid.com\/en\/wp-json\/wp\/v2\/forum-tag?post=18270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}