c# - Mutex not behaving as expected when loading Form settings -
c# - Mutex not behaving as expected when loading Form settings -
based on code project article on windows forms user settings david veeneman, have been playing around saving start location , start size of application.
it works in single instance, when expand multiple instance run problems.
i've wrapped section dealing loading setting , saving settings in mutex protect writing , settings file.
i windows stack lastly known location. seems work fine of time, if open 4 or 5 windows in rapid sucession first 3 open perfectly, there gap, , after of them start open @ same location.
render form/application:
private mutex savesetting; private const int start_location_offset = 20; private void mainform_load(object sender, eventargs e) { // mutex before reading of location // can't have situation window drawn // in wrong position. this.savesetting = new mutex(false, "savesetting"); this.savesetting.waitone(); this.loadwindowstartsizeandlocation(); . . . . . this.savesetting.releasemutex(); }
loading settings:
private void loadwindowstartsizeandlocation() { // set window location if (settings.default.windowlocation != null) { system.drawing.point startlocation = new system.drawing.point (settings.default.windowlocation.x + start_location_offset, settings.default.windowlocation.y + start_location_offset); this.location = startlocation; settings.default.windowlocation = startlocation; settings.default.save(); } // set window size if (settings.default.windowsize != null) { this.size = settings.default.windowsize; } }
saving settings:
private void mainform_formclosing(object sender, formclosingeventargs e) { seek { this.savewindowsizeandlocationfornextstart(); } grab (exception ex) { system.diagnostics.debug.assert(false, ex.message); } } /// <summary> /// save window size , location next application start up. /// </summary> private void savewindowsizeandlocationfornextstart() { if (this.windowstate != formwindowstate.minimized) { // re-create window location app settings settings.default.windowlocation = this.location; settings.default.windowsize = this.size; } seek { this.savesetting = new mutex(false, "savesetting"); this.savesetting.waitone(); settings.default.save(); } grab { // nothing. won't permanently disable scheme if // can't save settings. } { this.savesetting.releasemutex(); } }
can tell me i'm doing worng? or how based on code above 2 instances rendered same start location??
thanks a
the problem settings loaded before mutex acquired. phone call settings.default.reload()
after acquiring mutex.
c# application-settings multiple-instances
Comments
Post a Comment