c# - Using Auto reset event is not working as expected -
i quite new c# programming, trying achieve following result failing so.
what expect -: on click event of button, want open applciation via api, run analysis , exit application. while running application have progress bar on form should keep going 0 - 100 till runanalysis()
method called through api gets executed, when gets executed progress bar should show 100% , application called through should exit
what happening -:
the runanalysis()
being executed , application exits, click event of button gets executed , progress bar moves 0 - 100 should not happen
what attempt
namespace trialapp { public partial class form1 : form { autoresetevent obj = new autoresetevent(false); public form1() { initializecomponent(); } etabs2015.csapmodel sapmodel; system.reflection.assembly etabsassembly; etabs2015.coapi etabsobject; int result = -1; delegate int mydelegate(); mydelegate pointer = null; private void button1_click(object sender, eventargs e) { //use ret check return values of oapi calls int ret; //dynamically load etabs.exe assembly program installation folder string pathtoetabs = system.io.path.combine(environment.getenvironmentvariable("programfiles"), "computers , structures", "etabs 2013", "etabs.exe"); etabsassembly = system.reflection.assembly.loadfrom(pathtoetabs); //create instance of etabsobject , reference coapi interface etabsobject = (etabs2015.coapi)etabsassembly.createinstance("csi.etabs.api.etabsobject"); //start etabs application ret = etabsobject.applicationstart(); //get reference csapmodel access oapi classes , functions sapmodel = etabsobject.sapmodel; //initialize model ret = sapmodel.initializenewmodel(); //create steel deck template model ret = sapmodel.file.newsteeldeck(4, 12, 12, 4, 4, 24, 24); //save model system.io.directory.createdirectory("c:\\etabsapi"); ret = sapmodel.file.save("c:\\etabsapi\\example2.edb"); //run analysis backgroundworker1.runworkerasync(); // ret = sapmodel.analyze.runanalysis(); obj.waitone(); //close etabs ret = etabsobject.applicationexit(false); //clean variables sapmodel = null; etabsobject = null; //check ret value if (ret == 0) { messagebox.show("api script completed succesfully."); } else { messagebox.show("api script failed complete."); } } public void afterrunanalysiscomplete(iasyncresult resultholder) { result = pointer.endinvoke(resultholder); } private void backgroundworker1_dowork(object sender, doworkeventargs e) { pointer = new mydelegate(sapmodel.analyze.runanalysis); iasyncresult flag = pointer.begininvoke(new asynccallback(afterrunanalysiscomplete), null); while (!flag.iscompleted) { (int = 0; <= 100; i++) { thread.sleep(100); backgroundworker1.reportprogress(i); if (i == 100) { = 0; } if (flag.iscompleted) { break; } } } backgroundworker1.reportprogress(100); //obj.set(); } private void backgroundworker1_progresschanged(object sender, progresschangedeventargs e) { progressbar1.value = e.progresspercentage; } } }
can 1 tell me going wrong?
edit -:
i tried not using waitone()
, putting code followsbackgroundworker1.runworkerasync();
in backgroundworker1_dowork
method, not want extent of main project , not make sense design of classes.
the problem, see it, when kick off
ret = etabsobject.applicationstart();
it's going start in new thread, program isn't going have access to. recommend starting application in taskfactory, can use while check if task still running , update progress bar.
maybe this:
var etabapp = task.factory.startnew(() => { etabsobject.applicationstart()}); while(etabapp.status == taskstatus.running) { //do check percent complete , update progress bar }
Comments
Post a Comment