【发布时间】:2012-04-23 07:52:12
【问题描述】:
我正在使用 WPF。我使用 Dispatcher 在主 xaml 窗口中运行线程进程。
然后我在打开另一个 WPF 以显示显示 3D 图像的结果时收到此错误:
{"调用线程必须是STA,因为很多UI组件需要 这个。”}
这就是我打开新窗口的方式:
void DisplayFormThread()
{
IResult result = _mainWindow.GetResult();
ResultView resultView = new ResultView (result);
resultView.Show();
}
我试图通过在 stackoverflow 上的一些帖子中添加这个来解决问题,但它没有帮助:
ThreadStart start = delegate()
{
DispatcherOperation op = Dispatcher.CurrentDispatcher.BeginInvoke(
new delegateNewWindow(DisplayFormThread), DispatcherPriority.Background);
DispatcherOperationStatus status = op.Status;
while (status != DispatcherOperationStatus.Completed)
{
status = op.Wait(TimeSpan.FromMilliseconds(1000));
if (status == DispatcherOperationStatus.Aborted)
{
// Alert Someone
}
}
};
// Create the thread and kick it started!
Thread thread = new Thread(start);
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
我该如何解决这个问题?
提前致谢。
【问题讨论】:
标签: wpf dispatcher