views:

55

answers:

1

I have a C# WinForms application. This exception is thrown within the static void Main() method when a DevExpress XtraMessageBox is displayed prior to starting up the main UI form. Below is the code (simplified):

static void Main(string[] args)
{
    // Display Splash Screen.
    SplashForm.Start();

    if (!CheckLicense())
        XtraMessageBox.Show(null, "Not Licensed!", "License Check",
            MessageBoxButtons.OK, MessageBoxIcon.Information);

    using (MainForm form = new MainForm())
    {
        SplashForm.Stop();

        if (form != null)
            Application.Run(form);
    }
}

While it is a DevExpress control, the exception is actually thrown on the call to:

System.Drawing.Graphics.get_PageUnit()

The exception is not thrown consistently. It is reproducable on a particular machine, but once I add a MicroSoft MessageBox.Show() ahead of the exception to display debug information, then I no longer get the exception. Here is the stack trace:

Object is currently in use elsewhere.
   at System.Drawing.Graphics.get_PageUnit()
   at DevExpress.Utils.Text.FontsCache.GetFontCacheByFont(Graphics graphics, Font font)
   at DevExpress.Utils.Text.FontsCache.GetStringSize(Graphics graphics, String text, Font font, StringFormat stringFormat, Int32 maxWidth)
   at DevExpress.Utils.Text.TextUtils.GetStringSize(Graphics g, String text, Font font, StringFormat stringFormat, Int32 maxWidth)
   at DevExpress.Utils.Paint.XPaintMixed.CalcTextSize(Graphics g, String s, Font font, StringFormat strFormat, Int32 maxWidth)
   at DevExpress.Utils.AppearanceObject.CalcTextSize(Graphics g, StringFormat sf, String s, Int32 width)
   at DevExpress.Utils.AppearanceObject.CalcTextSize(Graphics g, String s, Int32 width)
   at DevExpress.XtraEditors.Drawing.EditorButtonPainter.CalcCaptionSize(EditorButtonObjectInfoArgs e)
   at DevExpress.XtraEditors.Drawing.EditorButtonPainter.CalcObjectMinBounds(ObjectInfoArgs e)
   at DevExpress.XtraEditors.Drawing.SkinEditorButtonPainter.CalcObjectMinBounds(ObjectInfoArgs e)
   at DevExpress.XtraEditors.ViewInfo.BaseButtonViewInfo.CalcBestFit(Graphics g)
   at DevExpress.XtraEditors.BaseControl.CalcBestSize()
   at DevExpress.XtraEditors.XtraMessageBoxForm.CreateButtons()
   at DevExpress.XtraEditors.XtraMessageBoxForm.ShowMessageBoxDialog()
   at DevExpress.XtraEditors.XtraMessageBoxForm.ShowMessageBoxDialog(XtraMessageBoxArgs message)
   at DevExpress.XtraEditors.XtraMessageBox.Show(UserLookAndFeel lookAndFeel, IWin32Window owner, String text, String caption, DialogResult[] buttons, Icon icon, Int32 defaultButton, MessageBoxIcon messageBeepSound)
   at DevExpress.XtraEditors.XtraMessageBox.Show(IWin32Window owner, String text, String caption, DialogResult[] buttons, Icon icon, Int32 defaultButton, MessageBoxIcon messageBeepSound)
   at DevExpress.XtraEditors.XtraMessageBox.Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
   at DevExpress.XtraEditors.XtraMessageBox.Show(IWin32Window owner, String text, String caption, MessageBoxButtons buttons, MessageBoxIcon icon)
   at Test.Program.Main(String[] args)
A: 

As far as I understand, the Application.Run() needs to be specifically called before you show any form, since it starts up the window message loop/pump and basically spawns a separate thread for the UI.

If you don't do that, the form will not be able to process messages or paint.

My suggestion is, load up the main form and have the main form call the splash screen before it does any of its normal FormLoad stuff. If the licensing fails, you can call Application.Exit() and return out of FormLoad, thus shutting down your app before the user can use it.

Edit: Note that the main form will not show until after FormLoad exits, so you don't have to worry about hiding your main form while the splash screen is showing.

Edit 2: I found something worthwhile, using ApplicationContext. You can switch out which form is in the main context, so you can load up your splash screen in an initial application context, and then swap it out once it's loaded. Try this:

public class MyApplicationContext : ApplicationContext {
    SplashForm splashForm;
    MainForm mainForm;

    public MyApplicationContext() {
        splashForm = new SplashForm();
        base.MainForm = splashForm;

    }

    public void RunApplication() {
        // This will show the splash screen
        ThreadPool.QueueUserWorkItem(new WaitCallback(MessageLoopThread));

        // This will perform any miscellaneous loading functions
        splashForm.PerformLoadingFunctions();

        if (!CheckLicensing()) {
            ShowErrorMessage();
            Application.Exit();
            return;
        }

        // Now load the main form
        mainForm = new MainForm();

        // We're done loading!  Swap out our objects
        base.MainForm = mainForm;

        // Close our splash screen
        splashForm.Close();
        splashForm.Dispose();
        splashForm = null;
    }

    private void MessageLoopThread(object o) {
        Application.Run(this);
    }
}

Then you can call it in your main:

static void Main() {
    MyApplicationContext applicationContext = new MyApplicationContext();
    applicationContext.RunApplication();
}

I haven't tested this, but in theory it should work.

Edit 3: I realized there might be some thread safety issues here that you may have to work around as well. Check out the CodeProject article. It does it better than what I did here.

rakuo15
I have a complex form and just instantiating it can take 1-2 seconds. I need a splash screen that displays immediately. I have seen various online articles about displaying Splash screens prior to calling Application.Run(). Thus, I am very interested to know if it is in fact required to call Application.Run() first.
Elan
I've searched as well, and though I found [this article](http://msdn.microsoft.com/en-us/library/Aa446493), it doesn't quite do as you expect it to: it loads up the splash form in `Application.Run()` instead. What you do instead, if your form does a lot of processing, is call the splash form in the main form's constructor before any other call, that way it's the first thing that executes. Or you can use your `Application.Run()` on your splash screen and call the main form from there on a separate thread.
rakuo15
I've added some stuff that I've found in [this article](http://www.codeproject.com/KB/cs/applicationcontextsplash.aspx) on CodeProject. Hopefully that gets you somewhere.
rakuo15