c# - How to fix message boxes appearing behind splash screen? -


my winforms app's main window slow load (up 20 seconds, depending on arguments), needs splash screen.

the main window constructor slow because exercises thousands of lines of code (some of beyond influence). code pops message boxes.

i've tried 2 splash screen designs, each have problems. better ideas?

splash screen backgroundworker

static void main(string[] args) {     var splash = !args.contains("--no-splash");     if (splash)     {         var bw = new backgroundworker();         bw.dowork += (sender, eventargs) => showsplash();         bw.runworkerasync();     }      var app = new formmain(args); // slow. opens blocking message boxes.     application.run(app); }  private static void showsplash() {     using (var splash = new formsplash())     {         splash.show();         splash.refresh();         thread.sleep(timespan.fromseconds(2));     } } 

problems:

  1. splash screen expires before main window open (user thinks app has crashed)
  2. main window minimises when splash closes.

splash screen windowsformsapplicationbase

sealed class app : windowsformsapplicationbase {     protected override void oncreatesplashscreen()     {         this.splashscreen = new formsplash();     }      protected override void oncreatemainform()     {         // slow. opens blocking message boxes.         this.mainform = new formmain(this.commandlineargs);     } } 

problems:

  1. any messageboxes opened appear behind splash screen, silently. user won't notice , thinks app stuck.
  2. if splash screen 'always on top', message box inaccessible , unclickable.

i agree hans passant in code needs re-evaluated design seems incorrect.

as problem @ hand, should able resolve creating own instance of messagebox.

i tested using code;

public dialogresult topmostmessagebox(string message, string title, messageboxbuttons button, messageboxicon icon)     {         return displaymessagebox(message, title, button, icon);     }  public dialogresult displaymessagebox(string message, string title, messageboxbuttons buttons, messageboxicon icon)     {         dialogresult result;         using (var topmostform = new form {size = new system.drawing.size(1, 1), startposition = formstartposition.manual})         {             var rect = systeminformation.virtualscreen;             topmostform.location = new system.drawing.point(rect.bottom + 10, rect.right + 10);             topmostform.show();             topmostform.focus();             topmostform.bringtofront();             topmostform.topmost = true;             result = messagebox.show(topmostform, message, title, buttons, icon);             topmostform.dispose();         }         //you might not need these properties...         return result;     } //usage topmostmessagebox("message","title" messageboxbuttons.yesno, messageboxicon.question) 

again, need stress agree original code needs re-factored , providing possible solution question.

hope helps?


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -