繁体   English   中英

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

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

我正在使用WindowsFormsApplicationBase来显示启动画面。 现在,当创建主窗体并发生错误时,应显示一个消息框,通知用户。 但是,消息框显示在启动屏幕下,因此它不可见。 我需要关闭启动画面,以便与用户进行交互。

以下代码将给出跨线程操作异常:

class SingleInstanceApplication : WindowsFormsApplicationBase
{
    private static SingleInstanceApplication instance;

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

错误:

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

这有可能吗?

如果您的启动画面窗体名为mySplashScreen

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

您不能从创建它们的线程以外的线程访问用户界面元素。 通常可以使用Control.Invoke从其他线程访问UI元素。

暂无
暂无

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

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