2011-09-19 6 views
3

스플래시 화면을 표시하기 위해 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. 

이 가능하다조차를?

답변

3

은 당신의 시작 화면 양식은 mySplashScreen라는 이름의 경우 :

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

사용자 인터페이스 요소를 만든 스레드 이외의 스레드에서 사용자 인터페이스 요소에 액세스 할 수 없습니다. 일반적으로 Control.Invoke을 사용하여 다른 스레드의 UI 요소에 액세스 할 수 있습니다.

+1

이 작업을 수행하는 일반적인 방법은 UI 스레드에서 SynchronizationContext를 얻을 수 SynchronizationContext.Current을 사용하는 것입니다. 그런 다음 백그라운드 나 다른 스레드에서 작동하는 모든 것으로 전달합니다. 그러면 UI 스레드를 통해 대리인을 게시 할 수 있습니다. – Will

관련 문제