[英]Calling a static async method from a Page results in InteropServices.COMException error
I'm calling an async method which is located in App.xaml.cs file.我正在调用位于 App.xaml.cs 文件中的异步方法。 I'm calling this method from my c# file of home page (HomePageView.xaml.cs).我从主页的 c# 文件 (HomePageView.xaml.cs) 调用此方法。 I don't know why but I'm getting this error:我不知道为什么,但我收到此错误:
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
This exception is thrown on the line starting with TEXT1_STRING
. System.Runtime.InteropServices.COMException: 'The application called an interface that was marshalled for a different thread. (0x8001010E (RPC_E_WRONG_THREAD))'
此异常在以TEXT1_STRING
开头的行上抛出。
The UpdateText()
method is: UpdateText()
方法是:
private async void UpdateText()
{
HomePageViewText1.Text = "error text";
TEXT1_STRING = Task.Run(() => App.GetJSONAsync()).Result;
return;
}
GetJSONAsync()
method code can be found below: GetJSONAsync()
方法代码如下:
internal static async Task<string> GetJSONAsync()
{
string JSON = null;
using HttpClient client = new HttpClient();
try
{
JSON = await client.GetStringAsync("https://www.example.com/api/getdata");
}
catch (Exception e)
{
log.Error("Error fetching JSON" + e.ToString());
}
return JSON;
}
Can anyone help me why this is happening?任何人都可以帮助我为什么会这样吗? I'm not very familiar with Threading, Marshals etc. with C#.我不太熟悉 C# 的线程、Marshals 等。 Thank you.谢谢你。
I've solved this exception by applying @MM's solution.我已经通过应用@MM 的解决方案解决了这个异常。 What I did was to return TEXT1_STRING = Task.Run(() => App.GetJSONAsync()).Result;
我所做的是返回TEXT1_STRING = Task.Run(() => App.GetJSONAsync()).Result;
to TEXT1_STRING = await App.GetJSONAync();
到TEXT1_STRING = await App.GetJSONAync();
After this change, the exception was not thrown and program was able to build and run successfully.进行此更改后,未抛出异常并且程序能够成功构建和运行。
Fixed version of UpdateText()
: UpdateText()
的固定版本:
private async void UpdateText()
{
HomePageViewText1.Text = "error text";
TEXT1_STRING = await App.GetJSONAync();
return;
}
The first function should be:第一个函数应该是:
private async Task UpdateText()
{
HomePageViewText1.Text = "error text";
TEXT1_STRING = await GetJSONAsync();
return;
}
As an async
function it should contain an await
statement.作为一个async
函数,它应该包含一个await
语句。 Trying to make an async function run synchronously, by using Task(....).Result()
or otherwise, is a bit of a minefield, see this blog for some explanation.尝试通过使用Task(....).Result()
或其他方式使异步函数同步运行有点雷区,请参阅此博客以获取一些解释。
See also async/await - when to return a Task vs void?另见async/await - 何时返回 Task 与 void? -- exception handling is different for async void
functions than async Task
functions. -- async void
函数的异常处理不同于async Task
函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.