c# - killing a thread from within in a form -


my application calls crystal reports viewer display report. run report viewer in separate thread. works fine. displays report properly. problem want kill report while processing if taking long run. while report processing busy indicator spinning , seems block ui on report viewer form. report viewer form has crystal reports viewer on along close button @ bottom of form itself. able click close button , have stop processing. here code run viewer in single apartment thread

 public void runreportstep1(uareport report)     {         uareportservice service = new uareportservice(report);         service.runreport();         var reportdocument = service.reportdocument;            thread stathread = new thread(r => { runreportstep2((reportdocument) r); });         stathread.setapartmentstate(apartmentstate.sta);         stathread.start(reportdocument);         stathread.join();     }      public void runreportstep2(reportdocument reportdocument)     {         reportviewerform form = new reportviewerform(reportdocument);         form.bringtofront();         form.showdialog();         if (form.dialogresult == dialogresult.ok)         {          } 

what best way kill thread within report viewer form while processing going on. once processing completes , report destroyed closing report no problem. it's problem while processing going on before report displayed. while report processing close button not responsive. if click repeatedly can response , report cancels. not consistent , have click repeatedly. not acceptable clients have do.

call service.runreport() thread , wait thread finish or amount of time pass. didn't write code this, didn't write least described.

    // global can reached both threads     uareportservice service;      // global variable written when report doc ready:     reportdoctype reportdoc; //can't use var here unfortunately      public void runreportstep1(uareport report)     {         service = new uareportservice(report);         thread stathread = new thread(r => { runreportstep2((uareportservice)r); });         stathread.setapartmentstate(apartmentstate.sta);         stathread.start(service);          // save time thread started:         datetime start = datetime.now;          // display loading box (label or something, you).          // function end , user can stuff in ui again (like press cancel).         myloadingbox.visible = true;     }      public void runreportstep2(uareportservice service)     {         service.runreport();         reportdocument = service.reportdocument;     }      // call new function periodically see if service thread finished, maybe once every second.     public checkanddisplay()     {         // if thread finished or 30 seconds have passed.         if (stathread.isalive == false || (datetime.now - start).totalseconds > 30)         {             // show message saying report failed run         }         else // else can show report viewer         {             reportviewerform form = new reportviewerform(reportdocument);             form.bringtofront();             form.showdialog();             if (form.dialogresult == dialogresult.ok)             {              }         }     }      // code cancelling report service call if user presses cancel button.     private void cancelbutton_click(object sender, eventargs e)     {         // terminate report service thread.         stathread.abort();          // abort() isn't nicest way if can set timeout on uareportservice object (where quits running after long) better.          // or have periodically check variable see if should quit (see link below)     } 

msdn example of gracefully closing thread.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -