C#WinFormsアプリケーションがあります。この例外は、メインUIフォームを起動する前にDevExpress XtraMessageBoxが表示されたときに、静的なvoid Main()メソッド内でスローされます。以下はコードです(簡略化):
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);
}
}
これはDevExpressコントロールですが、実際には次の呼び出しで例外がスローされます。
System.Drawing.Graphics.get_PageUnit()
例外は一貫してスローされません。特定のマシンで再現できますが、例外の前にMicroSoft MessageBox.Show()を追加してデバッグ情報を表示すると、例外は発生しなくなります。スタックトレースは次のとおりです。
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)
更新: UI作業を行う前に、Application.Run()が実行されていることを確認して解決しました。このようにして、メッセージループ/ポンプが起動します。これで、Application.Run()がスプラッシュフォームを起動します。これは軽量で高速です。次に、スプラッシュフォーム内からメインフォームをインスタンス化し、アクティブにして、スプラッシュフォームを非表示にします。