【发布时间】:2013-12-15 12:21:53
【问题描述】:
这是一个调用自定义 C# 消息框的函数。该函数是从一个模拟进程中调用的,该进程在一个不是 UI 线程的线程内运行。
public Output.ButtonResult msgboxYesNo(string Text, string title) {
Output.Message_Box msg = new Output.Message_Box();
msg.Dispatcher.Invoke(new Action(() => {
msg.seeQuestion(Text, title);
msg.Topmost = true;
Application.Current.MainWindow.Dispatcher.Invoke(new Action(()
=> { msg.Owner = Application.Current.MainWindow; }));
msg.ShowDialog();
}));
return msg.result;
}
有问题的行是这一行:
Application.Current.MainWindow.Dispatcher.Invoke(new Action(() =>
{ msg.Owner = Application.Current.MainWindow; }));
它抛出这个:
调用线程无法访问该对象,因为不同的 线程拥有它
因为我想将主窗口设置为自定义消息框的所有者,在单独的线程中调用。
如何将主窗体设置为消息框的所有者?
(我希望我把问题解释得足够清楚,表格是 WPF)
【问题讨论】:
标签: c# wpf multithreading