异步调用第三个部分代码 [英] Calling 3rd Part Code asynchronously

查看:89
本文介绍了异步调用第三个部分代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自定义一些第三方代码。我想异步调用一些代码,这些代码目前在async / await中不起作用。

I am customizing some 3rd party code. I want to call some code asynchronously which is currently not working in async/await.

Ex: - 转换对象  - [(根)系统]启动根和系统是第三方代码。

Ex :- Casting an object  - [(Root)system] boot Root and system are 3rd party code.

注意: - 如果我同步运行它,这是有效的。

Note:- This works if i run it synchronously.

在async / await中引发异常,函数需要所有线程运行然后返回null引用异常

inside an async/await which throws exception, Function requires all threads to run and then null reference Exception.

这是否意味着它以某种方式涉及UI?因为我认为涉及UI的代码不能异步运行。

Does this means it involves UI somehow?? As i believe code involving UI cannot run asynchronously.

如果它不是UI相关的那么如何解决这个问题?

If it is not UI related then how to rectify this?

推荐答案

您获得的错误来自调试器,而不是代码。如果您在调试器中暂停代码然后尝试评估需要执行代码的内容,则调试器会显示此消息。它只是意味着在代码
取消暂停之前它无法对其进行求值。

The error you're getting is from the debugger, not the code. If you pause your code in the debugger and then try to evaluate something that requires code to execute then the debugger shows this message. It just means it cannot evalulate it until the code is unpaused.

空引用错误是您获得的实际运行时错误。很遗憾,我们无法访问您的代码,因此我们无法解释您为何获得空引用。明确的原因是你有一个空对象。看看你发布的代码,你就是
做演员表。如果`system`变量不是`Root`类型,你会得到一个强制转换异常,但是如果它是null,那么你赋予它的任何东西都是null。您需要查看异常上的callstack以查看代码中的哪个位置失败,并将
指向您引用的实例为null。在那里,您可以追溯所有代码路径以确认您正在为其分配(有效)值并且它的行为正确。设置断点有助于解决这个问题。

The null reference error is the actual runtime error you're getting. Unfortunately we don't have access to your code so we cannot explain why you'd get a null reference. The clear reason is that you have a null object. Looking at your posted code you're doing a cast. If the `system` variable isn't of type `Root` you'd get a cast exception but if it is null then whatever you assigned it to is null. You'll need to look at the callstack on the exception to see where in your code it is failing and that will point you to the instance you're referencing is null. Once there you can trace back through all the code paths to confirm that you are assigning it a (valid) value and that it is behaving correctly. Setting breakpoints help with this.

对于UI事物,如果你在非UI线程上引用需要UI线程的对象,那么它最终会抛出异常。如果您在调试器中,您将从调试器中获得非常具体的跨线程错误。这没有发生在这里
但这并不意味着一旦你修复了空引用错误它就不会。如果必须在创建它的线程上调用您引用的组件,则无法在任何其他线程上引用它。只有你和组件的开发人员才能知道
的答案。

As for a UI thing then if you reference an object that requires the UI thread on a non-UI thread then it'll eventually throw an exception. If you're in the debugger you'll get a very specific cross thread error from the debugger. That hasn't happened here but that doesn't mean once you fix the null reference error it won't. If the component you're referencing must be called on the thread that created it then you cannot reference it on any other thread. Only you and the developer of the component can know the answer to that.

至于运行代码异步,这很容易。只需将其包装在Task.Run调用中即可。使用await等待它(如果是异步方法)或使用Wait()或Result等待它完成。

As for running code async it is pretty easy to do. Just wrap it in a Task.Run call. Either use await to wait for it (if in an async method) or use Wait() or Result to wait for it to complete.


这篇关于异步调用第三个部分代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆