c# - Run multiple winform instance sequentially -
i have c# winform application needs run multiple instance in synchronous way. goal to:
if exe runs 3 times, runs first instance of exe , rest wait until first instance finishes processing. then, next waiting exe intance run , process , on.
the exe run 1 one until finish processing exe terminates automatically af.
any idea how this?
i tried below:
private void checkinstance() { bool _returnvalue = true; string _lockfile = string.empty; random _rnd = new random(); int _randomvalue = _rnd.next(100, 200); int _rndmilisec = 0; _rndmilisec = datetime.now.millisecond * _rnd.next(2, 6); _lockfile = string.concat(appdomain.currentdomain.basedirectory, string.format("/{0}", instancefilename)); while (_returnvalue) { _returnvalue = file.exists(_lockfile); if (_returnvalue) { thread.sleep(1000); this.hide(); } else { try { thread.sleep((_rnd.next(1000) + _rndmilisec) + _rnd.next(1000, 1500)); functions.writelog(_lockfile, "starting process..."); functions.writelog(_lockfile, string.format("start time : {0}", paramprinttime)); file.setattributes(_lockfile, fileattributes.readonly); this.show(); break; } catch (exception) { _returnvalue = false; } } } } private void deleteinstance() { try { file.setattributes(string.concat(appdomain.currentdomain.basedirectory, string.format("/{0}", instancefilename)), fileattributes.normal); file.delete(string.concat(appdomain.currentdomain.basedirectory, string.format("/{0}", instancefilename))); } catch (exception) { } } private void form_shown(object sender, eventargs e) { _backworker.runworkerasync(); } private void formclosed(object sender, formclosedeventargs e) { deleteinstance(); } private void form_load(object sender, system.eventargs e) { checkinstance(); } backgroundworker _backworker = new backgroundworker(); public form() { initializecomponent(); _backworker.workerreportsprogress = true; _backworker.progresschanged += _backworker_progresschanged; _backworker.runworkercompleted += _backworker_runworkercompleted; _backworker.dowork += _backworker_dowork; } private void _backworker_dowork(object sender, doworkeventargs e) { work processing... } private void _backworker_runworkercompleted(object sender, runworkercompletedeventargs e) { this.close(); } private void _backworker_progresschanged(object sender, progresschangedeventargs e) { pg.value = e.progresspercentage; lblindicator.text = e.userstate.tostring(); } when exe run 3 instance, first instance run while 2nd , third hides while awaiting 1st instance finisih. however, after 1st instance finish process, 2nd , 3rd instance running simultaneously.
any ideas? thanks.
maybe can work:
public static bool isprogramrunning(string titleofyourform) { bool result = false; process[] processes = process.getprocesses(); foreach (process p in processes) { if (p.mainwindowtitle.contains(titleofyourform)) { result = true; break; } } return result; } call function in main function(before opening mainform), if false application.exit() else show form.. if answer helped you, vote me.
Comments
Post a Comment