c# - Reactive Threading Exception: 调用线程必须是 STA,因为很多 UI 组件都需要这个
问题描述
我正在使用以下代码
public IObservable<Unit> RunReport()
{
return Observable.Start(() =>
{
string selectedPath = null;
//System.Windows.Application.Current.Dispatcher.Invoke(delegate
//{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
selectedPath = ofd.FileName;
}
else
{
selectedPath = null;
}
//});
});
}
这会引发The calling thread must be STA, because many UI components require this
异常。
如果我取消注释上面的 3 行,那么我不会得到异常,但这感觉不是解决这个问题的正确方法,因为我看到的关于 Observables 和线程问题的许多其他答案建议使用Observable.Start(() => ...).ObserveOnDispatcher();
.
我已经尝试过该选项,但错误仍然存在!
解决方案
推荐阅读
- android - 具有可变片段大小的 Android 布局
- android - Android Overlay View 让一些应用程序暂停
- javascript - Javascript深拷贝(值)在数组中不起作用
- sql - SQL Server 条件 where 子句
- python - 选择在带有 ComboBox 的 QTableWidget 中不起作用 - 复制事件未触发
- kendo-ui - 如何异步绑定到 Kendo UI 控件?
- sql - 在 Amazon Athena 中按索引选择行
- python - OSError: [Errno 27] 文件太大
- html - 跨浏览器 SVG 文本基线对齐
- java - 弹性搜索在从另一个索引创建索引时丢失了一些文档