단추 시작을 클릭 한 후 wpf 컨트롤 상태를 변경하고 싶습니다.스레드 시작 전에 WPF 컨트롤 상태를 변경할 수 없습니다.
사진이 내가 원하는 것입니다. 다음
내 코드
private bool _bWorking = false;
public delegate void UpdateStatusDelegate();
private void SetStatus(bool bEnable)
{
if (bEnable)
{
tbName.IsReadOnly = false;
barStatus.Visibility = Visibility.Hidden;
btnStart.IsEnabled = true;
btnStop.IsEnabled = false;
btnClose.IsEnabled = true;
}
else
{
tbName.IsReadOnly = true;
barStatus.Visibility = Visibility.Visible;
btnStart.IsEnabled = false;
btnStop.IsEnabled = true;
btnClose.IsEnabled = false;
}
}
internal void UpdateStatus()
{
SetStatus(true);
_bWorking = false;
}
private void ThreadFunc()
{
//for (; ;)
//{
// // do something here
// if (_bWorking == false)
// break;
//}
Thread.Sleep(500);
this.Dispatcher.Invoke(new UpdateStatusDelegate(UpdateStatus));
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
_bWorking = true;
SetStatus(false);
this.UpdateLayout();//this.InvalidateVisual();
try
{
Thread t = new Thread(new ThreadStart(() =>
{
ThreadFunc();
}));
t.IsBackground = true;
t.Name = "test status";
t.Start();
while (t.IsAlive)
{
// wait thread exit
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
_bWorking = false;
SetStatus(true);
}
하지만 버튼의 시작을 클릭 한 후 실제로 UI 냉동 보인다, 그리고 스레드가 정상이 될 UI를 종료.
내 VS는 VS2010입니다.
Force a WPF control to refresh?
이 게시물은 나를 위해 작동하지 않습니다.
편집 요약 :
추가 위임 무효 UpdateStatusDelegate()와 기능 UpdateStatus() 내 코드에
가능한 중복 [C#에서 다른 스레드에서 GUI를 업데이트하는 방법?] (http://stackoverflow.com/questions/661561/how-to -update-the-gui-from-another-thread-in-c) – Gusdor
고마워,하지만 그것은 나를 위해 작동하지 않습니다. 그 게시물은 WinForm에 대한 이야기, 내 문제는 WPF에 관한 것입니다. – Rock