Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从其他线程关闭启动屏幕?_C#_.net_Winforms_Multithreading - Fatal编程技术网

C# 从其他线程关闭启动屏幕?

C# 从其他线程关闭启动屏幕?,c#,.net,winforms,multithreading,C#,.net,Winforms,Multithreading,我用它来显示闪屏。现在,当创建主窗体并且出现错误时,应该显示一个消息框,通知用户。但是,messagebox显示在启动屏幕下,因此不可见。我需要关闭启动屏幕,以便与用户交互 以下代码将给出一个跨线程操作异常: class SingleInstanceApplication : WindowsFormsApplicationBase { private static SingleInstanceApplication instance; public static void Cl

我用它来显示闪屏。现在,当创建主窗体并且出现错误时,应该显示一个消息框,通知用户。但是,messagebox显示在启动屏幕下,因此不可见。我需要关闭启动屏幕,以便与用户交互

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

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.

这可能吗???

除了创建用户界面元素的线程之外,您不能从其他线程访问它们。通常可以从其他线程访问UI元素。

如果您的启动屏幕窗体名为
mySplashScreen

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

一般的方法是使用SynchronizationContext.Current来获取UI线程SynchronizationContext。然后将其传递给在后台或其他线程上工作的任何对象。然后,他们可以通过它将委托发布到UI线程。