开发者

Closing the splash screen from a different thread?

开发者 https://www.devze.com 2023-04-05 16:46 出处:网络
I\'m using WindowsFormsApplicationBase to show a splash screen. Now, when the main form is created, and errors occur, a messagebox should be shown informing the user. However, the messagebox is displa

I'm using WindowsFormsApplicationBase to show a splash screen. 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.SplashS开发者_如何学Ccreen != 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.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消