당신이 다중 스레드 수요가 발생하는 경우 WPF 애플리케이션에서, 이상, 이상 내용의 경우 참조 WPF 컨트롤로 이어질 것입니다 : 다른 스레드가 객체를 소유하고 있기 때문에 호출 스레드가이 개체에 액세스 할 수 없습니다. 호출 코드 : 다음 ThreadcountThread = 새로운 스레드 (신규 ThreadStart ()는 카운트); countThread.Start ()을, 이하의 방법 호출 개시제의 개수
당신이 다중 스레드 수요가 발생하는 경우 WPF 애플리케이션에서, 이상, 이상 내용의 경우 참조 WPF 컨트롤로 이어질 것입니다 : 다른 스레드가 객체를 소유하고 있기 때문에 호출 스레드가이 개체에 액세스 할 수 없습니다. 다음과 같습니다 :
코드를 호출 :
countThread = 스레드
새로운 Thread (새 ThreadStart () 카운트);
countThread.Start ();
다음이라는 계산 방법에서 예외를 발생
![](http://www.silverlightchina.net/uploads/allimg/110404/022UR4H-0.jpg)
WPF 개체 동시 스레드 처리를위한 기본 구조를 제공 DispatcherObject에로부터 유도된다. WPF 기반 메시징 시스템 스케줄러의 구현입니다. 그것은 인기는 Win32 메시지 펌프를 작동하는 것은 매우 유사합니다, 사실, WPF 스케줄러 스레드 간 호출을 수행하기 위해 User32 메시지를 사용합니다. UI WPF 사용자 스레드를 업데이트 할 때, 디스패처를 통해 수행된다.
호출은 다음 코드를 참조하십시오 :
네임 스페이스 WpfThreadTest
{
/// <요약>
/// MainWindow.xaml的交互逻辑
/// </ 요약>
공공 부분 클래스 MainWindow를 : 윈도우
{
스레드 countThread;
공공 MainWindow를 ()
{
InitializeComponent를 ();
this.textBox1.Text = DateTime.Now.ToLocalTime의 toString () ( "YYYY MM年月日DD HH : MM : SS").; ;
countThread = 새로운 스레드 (새 ThreadStart (DispatcherThread));
}
개인 무효 button3_Click (객체 송신자 RoutedEventArgs E)
{
경우 (button3.Content.ToString () == "开始时间线程")
{
button3.Content는 = "스레드 정지 시간";
IF (countThread.ThreadState == ThreadState.Suspended)
{
// 스레드가 계속
countThread.Resume ();
}
다른
countThread.Start ();
}
다른
{
button3.Content = "시작 시간 스레드 ";
// 스레드 서스펜션
countThread.Suspend ();
}
}
공개 무효 DispatcherThread는 ()
{
UI를 업데이트 // 사이클링 조건에 의해 제어 될 수있다
(사실까지) 동안
{
/// 스레드 우선 순위 최대 시간 제한 방법 대리자 (NO 레퍼런스 법)
textBox1.Dispatcher.Invoke (
DispatcherPriority.Normal, TimeSpan.FromSeconds (1) 새로운 새 액션 (UpdateTime).)
스레드. SLEEP (1000);
}
}
개인 무효 UpdateTime ()
{
. this.textBox1.Text DateTime.Now.ToLocalTime = () 있으며, toString ( "날짜 YYYY, MM-DD HH : MM : SS");
}
개인 무효 WINDOW_CLOSED (개체 송신자있는 EventArgs E)
{
countThread.Abort ();
Application.Current.Shutdown ();
}
}
}
{
/// <요약>
/// MainWindow.xaml的交互逻辑
/// </ 요약>
공공 부분 클래스 MainWindow를 : 윈도우
{
스레드 countThread;
공공 MainWindow를 ()
{
InitializeComponent를 ();
this.textBox1.Text = DateTime.Now.ToLocalTime의 toString () ( "YYYY MM年月日DD HH : MM : SS").; ;
countThread = 새로운 스레드 (새 ThreadStart (DispatcherThread));
}
개인 무효 button3_Click (객체 송신자 RoutedEventArgs E)
{
경우 (button3.Content.ToString () == "开始时间线程")
{
button3.Content는 = "스레드 정지 시간";
IF (countThread.ThreadState == ThreadState.Suspended)
{
// 스레드가 계속
countThread.Resume ();
}
다른
countThread.Start ();
}
다른
{
button3.Content = "시작 시간 스레드 ";
// 스레드 서스펜션
countThread.Suspend ();
}
}
공개 무효 DispatcherThread는 ()
{
UI를 업데이트 // 사이클링 조건에 의해 제어 될 수있다
(사실까지) 동안
{
/// 스레드 우선 순위 최대 시간 제한 방법 대리자 (NO 레퍼런스 법)
textBox1.Dispatcher.Invoke (
DispatcherPriority.Normal, TimeSpan.FromSeconds (1) 새로운 새 액션 (UpdateTime).)
스레드. SLEEP (1000);
}
}
개인 무효 UpdateTime ()
{
. this.textBox1.Text DateTime.Now.ToLocalTime = () 있으며, toString ( "날짜 YYYY, MM-DD HH : MM : SS");
}
개인 무효 WINDOW_CLOSED (개체 송신자있는 EventArgs E)
{
countThread.Abort ();
Application.Current.Shutdown ();
}
}
}
실행 효과가 표시됩니다
![](http://www.silverlightchina.net/uploads/allimg/110404/022UQ093-1.jpg)