c# - Entering form modal message loop without actual form showing -


i have winforms application 1 main form mainform. application has scheduled operation being called in intervals. operation takes lot of time execute launched in background thread. when operation running form progress shown - loaderform. form shown modal via loader.showdialog(mainform); loader starts operation when shown.

this works fine unless operation starts when:

  • mainform minimized in taskbar
  • mainform hidden tray (not shown in taskbar @ all)

in such cases need not show loaderform unless application activated. problem loaderform shown when mainform not visually visible without proper handling.

what easiest way achieve desired behavior?


example code

imports system.componentmodel   public class mainform      private sub timer_tick(sender object, e eventargs) handles uitimer.tick         uitimer.enabled = false         using loader = new loaderform()             loader.showdialog(me)         end using     end sub  end class  public class loaderform      private sub loaderform_shown(sender object, e eventargs) handles mybase.shown         worker.runworkerasync()     end sub      private sub worker_runworkercompleted(sender object, e runworkercompletedeventargs) handles worker.runworkercompleted         me.close()     end sub  end class 

we can create new modal message loop without showing modal dialog calling undocumented win32 apis beginmodalmessageloop , endmodalmessageloop.

note: these apis undocumented, not supposed use them. should use these last resort.

    //capture method reference using reflection     _methodbeginloop = findmethod(typeof (application), "beginmodalmessageloop");     _methodendloop = findmethod(typeof (application), "endmodalmessageloop");         private static methodinfo findmethod(type type, string methodname)             {                 const bindingflags binding_flags = bindingflags.static | bindingflags.nonpublic;                  memberinfo[] members = type.findmembers(membertypes.method, binding_flags,                     new methodfilter(methodname).matches, null);                  if (members.length == 1)                     return members[0] methodinfo;                 return null;             } 

for blocking inputs on main form, can use

application.openforms[0].invoke(new action(() =>  {    _methodbeginloop.invoke(null, null);  } 

for unblocking inputs on main form, can use

application.openforms[0].invoke(new action(() =>  {    _methodendloop.invoke(null, null);  } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -