-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Closed
Labels
🤔 pending discussionIt may take time to discuss.It may take time to discuss.
Description
有的情况下,为了避免模态窗口弹出时无法操作别的窗口,会对个别窗口使用新的线程运行。
示例代码:
public static class DispatcherBuilder
{
public static Dispatcher Build()
{
Dispatcher dispatcher = null;
var manualResetEvent = new ManualResetEvent(false);
var thread = new Thread(() =>
{
dispatcher = Dispatcher.CurrentDispatcher;
var synchronizationContext = new DispatcherSynchronizationContext(dispatcher);
SynchronizationContext.SetSynchronizationContext(synchronizationContext);
manualResetEvent.Set();
try
{
Dispatcher.Run();
}
catch
{
// ignore
}
}, maxStackSize: 1);
thread.Priority = ThreadPriority.Normal;
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
manualResetEvent.WaitOne();
manualResetEvent.Dispose();
return dispatcher;
}
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
var dispatcher = DispatcherBuilder.Build();
dispatcher.InvokeAsync(() =>
{
try
{
MainWindow mainWindow = new MainWindow();
mainWindow.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
});
}
WPF原生风格没有这个问题。
示例代码(threadtest分支):https://github.com/cuiliang/HandyControl/tree/threadtest
Metadata
Metadata
Assignees
Labels
🤔 pending discussionIt may take time to discuss.It may take time to discuss.