- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我几乎在互联网上到处搜索,我用谷歌搜索了很多次,找到了很多结果,但我仍然找不到解决我问题的方法。
我正忙于将旧的 WinForms
应用程序转换为新的 WPF 应用程序,但我在使用某些命令时遇到了问题。在 Winforms 应用程序中,他们使用 Control.BeginInvoke()
并将其存储在 IAsyncResult 对象中。我读到 Dispatcher.BeginInvoke()
是 WPF
等同于 WinForms
的 Control.BeginInvoke()
> 但是当我使用
Dispatcher.BeginInvoke(): "Cannot implicitly convert type 'System.Windows.Threading.DispatcherOperation' to 'System.IAsyncResult'. An explicit conversion exists (are you missing a cast?)".
我们将不胜感激。
这是我要转换的代码。这是原始的 WinForms
代码。我能够转换除 BeginInvoke 部分以外的所有内容。
private eSkan.api.TeSkanAPI feSkanAPI = null;
private void MessageFilter_AddRemove_Invoked(bool AddFilter, IMessageFilter Filter)
{
if (AddFilter){ Application.AddMessageFilter(Filter); }
else { Application.RemoveMessageFilter(Filter); }
}
private void MessageFilter_AddRemove(bool AddFilter, IMessageFilter Filter)
{
{
IAsyncResult sr = BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked,
AddFilter, Filter);
sr.AsyncWaitHandle.WaitOne(2000);
}
}
下面是我到目前为止转换的代码,包括我正在努力处理的 BeginInvoke 部分。
private void MessageFilter_AddRemove_Invoked(bool addFilter, System.Windows.Forms.IMessageFilter filter)
{
if (addFilter)
{
System.Windows.Forms.Application.AddMessageFilter(filter);
}
else
{
System.Windows.Forms.Application.RemoveMessageFilter(filter);
}
}
private void MessageFilter_AddRemove(bool addFilter, System.Windows.Forms.IMessageFilter filter)
{
{
IAsyncResult sr = System.Windows.Threading.Dispatcher.BeginInvoke((ESKAN_ADD_REMOVE_MESSAGEFILTER)MessageFilter_AddRemove_Invoked, addFilter, filter);
sr.AsyncWaitHandle.WaitOne(2000);
}
}
如果还有其他错误,请告诉我。
谢谢
最佳答案
那是因为Dispatcher.BeginInvoke
,虽然它可能是等效的逻辑操作,但不返回 IAsyncResult
,它返回 DispatcherOperation
.看看this blog post您将看到一个很好的例子,说明 Dispatcher
是如何工作的。我已将相关代码示例复制到此处以确保它稍后存在。
public Window1()
{
InitializeComponent();
CheckBox myCheckBox = new CheckBox();
myCheckBox.Content = "A Checkbox";
System.Threading.Thread thread = new System.Threading.Thread(
new System.Threading.ThreadStart(
delegate()
{
System.Windows.Threading.DispatcherOperation
dispatcherOp = myCheckBox.Dispatcher.BeginInvoke(
System.Windows.Threading.DispatcherPriority.Normal,
new Action(
delegate()
{
myCheckBox.IsChecked = true;
}
));
dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);
}
));
thread.Start();
}
void dispatcherOp_Completed(object sender, EventArgs e)
{
Console.WriteLine("The checkbox has finished being updated!");
}
注意这一行:
dispatcherOp.Completed += new EventHandler(dispatcherOp_Completed);
这就是您将知道它何时完成的方式 - 它会通过该事件给您回电。
关于c# - 如何正确使用 Dispatcher.BeginInvoke?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15926310/
我的密码 https://gist.github.com/ButuzGOL/707d1605f63eef55e4af 因此,当我收到登录成功回调时,我想进行重定向, 重定向也可以通过调度程序进行。 我
我已经写了访问者模式如下,但我不明白什么是单次和双次分派(dispatch)。AFAIK,单分派(dispatch)是根据调用者类型调用方法,而双分派(dispatch)是根据调用者类型和参数类型调用
我有一个非 ui 线程,我需要在该线程上发送消息。 执行此操作的正常方法是在我的线程的线程过程中调用 Dispatcher.Run()。 我想修改它以使其在处理未处理的异常方面更加健壮。 我的第一个剪
我有一个具有这样功能的代码 const mapDispatchToProps = (dispatch: Dispatch) => ({ onAddProduct: ( key: str
我在使用 Window.Show 显示 WPF 窗口时遇到问题: System.InvalidOperationException was unhandled Message: An unhandle
我对何时使用 Dispatcher.Invoke 从不同线程更新 UI 上的某些内容存有疑问。 这是我的代码... public Window4() { InitializeC
我遇到了一个我无法解决的问题。我正在构建一个电子商务 react 应用程序并使用 useReducer 和 useContext 进行状态管理。客户打开产品,挑选商品数量,然后单击“添加到购物车”按钮
尽管我已经深入了解了 NEventStore 上的事务完整性,但我无法理解在连接了许多 NEventStore 实例时 NEventStore 将如何真正扩展。 总结一下我的理解,一个事件被添加到提交
我学习了 React Javascript 和 Redux,现在我遇到了这个问题。 这是一个 codesandbox 像这样尝试: 搜索书名“dep” 观察日志显示“Search url is:”,当
Dispatcher.CurrentDispatcher(在System.Windows.Threading中)和Application.Current.Dispatcher(在 >系统.Window
我得到了一些代码来处理调度程序在其构造函数中传递给 View 模型的位置。我现在想知道当我想要在 UI 线程上执行某些操作时,我是否应该使用 ObserveOn(dispatcher) 或 dispa
当我们的一个应用程序服务器内存不足时,我正在分析 Java 堆转储。我正在使用 Eclipse 内存分析器。它报告了以下内容。 One instance of "akka.dispatch.Dispa
哪一个: public static let barrier: DispatchWorkItemFlags public static let detached: DispatchWorkItem
我想使用不同于调度类型的类型提示 Action 创建者。 我已经尝试使用这两种类型对 ThunkResult 进行类型提示,但这并不理想。 // types.ts interface AppListA
我正在尝试准确地理解什么是单次分派(dispatch)和多次分派(dispatch)。 我刚刚读到这个: http://en.wikipedia.org/wiki/Multiple_dispatch
I have following api returning Flux of String我有以下返回字符串通量的接口 @GetMapping(value = "/api/getS
这是我自学前端开发一年后在Stackoverflow上的第一个问题。我已经找到了我的疑惑的答案,但由于这些问题是第三次返回,我认为是时候向 Web 提问了。 我正在尝试构建什么 我正在尝试构建一个图书
我正在使用 Kotlin 学习 Android,并且我了解到在不阻塞主线程的情况下启动协程的推荐方法是执行以下操作 MainScope().launch { withContext(Dispatc
错误本身: (alias) deleteCategory(id: number): (dispatch: Dispatch) => void import deleteCategory Argumen
我必须对抽屉进行裁剪,然后创建一个包含所有需要项的DrawerComponent,并创建一个带有NavigationActions的函数来调度我的路线,但是它不起作用。当我单击任何项目时,我都会遇
我是一名优秀的程序员,十分优秀!