简体   繁体   English

从不同的线程关闭启动画面?

[英]Closing the splash screen from a different thread?

I'm using WindowsFormsApplicationBase to show a splash screen. 我正在使用WindowsFormsApplicationBase来显示启动画面。 Now, when the main form is created, and errors occur, a messagebox should be shown informing the user. 现在,当创建主窗体并发生错误时,应显示一个消息框,通知用户。 However, the messagebox is displayed under the splash screen, so it's not visible. 但是,消息框显示在启动屏幕下,因此它不可见。 I need to close the splash screen so I can interact with the user. 我需要关闭启动画面,以便与用户进行交互。

The following code will give a cross-thread operation exception : 以下代码将给出跨线程操作异常:

class SingleInstanceApplication : WindowsFormsApplicationBase
{
    private static SingleInstanceApplication instance;

    public static void CloseSplash()
    {
        if (instance.SplashScreen != null)
            instance.SplashScreen.Close();
    }
}

Error: 错误:

Cross-thread operation not valid: Control 'Splash' accessed from a thread 
other than the thread it was created on.

Is this even possible ?? 这有可能吗?

If your splash screen form is named mySplashScreen : 如果您的启动画面窗体名为mySplashScreen

mySplashScreen.Invoke(new MethodInvoker(delegate {
    mySplashScreen.Close();
    mySplashScreen.Dispose();
}));

You can't access user interface elements from a thread other than the one that created them. 您不能从创建它们的线程以外的线程访问用户界面元素。 Commonly one can use Control.Invoke to access UI elements from other threads. 通常可以使用Control.Invoke从其他线程访问UI元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM