backgound에서 많은 작업을 수행하는 wpf 응용 프로그램을 만들고 있지만 여전히 UI가 반응 적이어야하고 다양한 백그라운드 작업의 상태를 표시해야합니다. 또한 UI를 전혀 표시하지 않을 수도 있습니다.이 경우 주 양식의 인스턴스를 전혀 작성하지 않고 상태 메시지를 버려야합니다. 내가 시도했습니다 무엇백그라운드 스레드의 정보로 WPF 창을 업데이트하는 방법은 무엇입니까?
는 App.xaml에서
StartupUri="MainWindow.xaml"
을 제거하는 것입니다. 그런 다음 App.xaml.cs를, 나는이 필요한 경우, 기본 양식을 보여 주며, 모든 백그라운드 프로세스를 시작
`
public App()
{
Startup += new StartupEventHandler(App_Startup);
}
void App_Startup(object sender, StartupEventArgs e)
{
// Code which loads application settings is here
if (pf.ShowUI)
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
// The background processes will begin here.
}`
있습니다. 이 부분은 작동합니다.
은 UI의 배경에서 메시지를 전송하기 위해, 나는 아주 기본적인 메신저 구현했습니다 :
`
internal interface IMessageHandler
{
void ReceiveMessage(string message);
}
internal class Messenger
{
private static List<IMessageHandler> _handlers;
internal static void AddHandler(IMessageHandler handler)
{
_handlers.Add(handler);
}
internal static void RemoveHandler(IMessageHandler handler)
{
try
{
_handlers.Remove(handler);
}
catch (Exception ex) {}
}
internal static void Broadcast (string message)
{
foreach (IMessageHandler handler in _handlers)
{
handler.ReceiveMessage(message);
}
}
}`
주요 형태는 IMessageHandler 인터페이스를 구현을, 그리고 자신을 추가 Messenger를 시작할 때 처리기로 사용합니다. 주 양식으로 상태를 전송해야하는 모든 프로세스는 메신저의 Broadcast
메 시지를 호출하면됩니다.
내가 겪고있는 문제는 백그라운드 프로세스가 완료 될 때까지 메시지가 양식에 표시되지 않고 그때까지 UI가 잠겨 있다는 것입니다. 왜 내 백그라운드 프로세스 내 UI를 동결하는
`
public void ReceiveMessage(string message)
{
Dispatcher.Invoke(DispatcherPriority.Normal,
new Action<string>(AddText),
message);
}
private void AddText(string text)
{
Label myLabel = new Label();
myLabel.Content = text;
stackPanel1.Children.Add(myLabel);
if (stackPanel1.Children.Count > 5)
{
stackPanel1.Children.RemoveAt(0);
}
}`
다음과 같이
수신 메시지를 처리하는 UI의 코드는 무엇입니까? 그것을 막기 위해 무엇을 할 수 있습니까? 그리고 내 UI가 상태 메시지로 업데이트되지 않는 이유는 무엇입니까?
패턴을 MVVM으로 사용하여 뷰를 모델 데이터에 바인딩하는 것이 좋습니다. – HyLian