c# - Custom message box show time expiring before it runs out WinForms -


i have custom message box class :

public class autoclosemsb {     readonly system.threading.timer _timeouttimer;     readonly string _caption;      private autoclosemsb(string text, string caption, int timeout)     {         _caption = caption;         _timeouttimer = new system.threading.timer(ontimerelapsed,             null, timeout, system.threading.timeout.infinite);         messagebox.show(text, caption);     }     public static void show(string text, string caption, int timeout)     {         new autoclosemsb(text, caption, timeout);     }      private void ontimerelapsed(object state)     {         intptr mbwnd = findwindow("#32770", _caption);         if (mbwnd != intptr.zero)             sendmessage(mbwnd, wmclose, intptr.zero, intptr.zero);         _timeouttimer.dispose();     }      private const int wmclose = 0x0010;     [system.runtime.interopservices.dllimport("user32.dll", setlasterror = true)]     private static extern intptr findwindow(string lpclassname, string lpwindowname);     [system.runtime.interopservices.dllimport("user32.dll", charset = system.runtime.interopservices.charset.auto)]     private static extern intptr sendmessage(intptr hwnd, uint msg, intptr wparam, intptr lparam); } 

and i'm calling in few places 1 after :

autoclosemsb.show("bot 1 turn", "turns", thinktime); autoclosemsb.show("bot 2 turn", "turns", thinktime); autoclosemsb.show("bot 3 turn", "turns", thinktime); autoclosemsb.show("bot 4 turn", "turns", thinktime); autoclosemsb.show("bot 5 turn", "turns", thinktime); 

and variable thinktime getting value out of resources that's change it. if put 3000 milliseconds example display time display first 1 3 secs others wont shown duration close in 100-200 ms ( show , close instantly) why happening ? should reset value of variable after each message box shown ?

if you're clicking ok button in messagebox you're not getting rid of timer , such still fire after messagebox has been closed. due non-uniqueness of captions event handler in ontimerelapsed find messagebox , closes it. leads subsequent closing of messageboxes because there timer still needs fire.

the quick fix bug move code rid of timer event directly after messagebox instead of ontimerelapsed event:

private autoclosemsb(string text, string caption, int timeout) {     _caption = caption;     _timeouttimer = new system.threading.timer(ontimerelapsed,         null, timeout, system.threading.timeout.infinite);     // next call blocks, until either user     // or timer closes the messagbox     messagebox.show(text, caption);     // can stop timer     _timeouttimer.change(timeout.infinite, timeout.infinite);     // , dispose     _timeouttimer.dispose(); } 

the timerelapsedevent signals cleanup can start:

private void ontimerelapsed(object state) {     debug.writeline("on timer");     intptr mbwnd = findwindow("#32770", _caption);     if (mbwnd != intptr.zero)         sendmessage(mbwnd, wmclose, intptr.zero, intptr.zero);     // don't touch state here, signal continue } 

these changes achieve want , keeps flow of code clear.


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -