简体   繁体   English

从页面调用静态异步方法导致 InteropServices.COMException 错误

[英]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.

相关问题 C# AutoIt InteropServices.COMException 错误 - C# AutoIt InteropServices.COMException error InteropServices.COMException - InteropServices.COMException 将项目添加到 ObservableCollection 时出现 InteropServices.COMException - InteropServices.COMException when adding item to ObservableCollection 从ASP MVC控制器运行Word时保持获取InteropServices.COMException - Keep Getting InteropServices.COMException when running Word from ASP MVC Controller 为什么我在尝试从 C# 启动 QTP 时收到 InteropServices.COMException? - Why do I get an InteropServices.COMException when attempting to launch QTP from C#? 间歇性的“ InteropServices.COMException / ForwardCallToInvokeMember”在范围单元格上访问Value2 - Intermittent “InteropServices.COMException / ForwardCallToInvokeMember” accessing Value2 on Range cell com 异常 excel InteropServices.COMException - c# - com exception excel InteropServices.COMException - c# 如何在不安装Excel的情况下修复InteropServices.COMException? - How can I fix InteropServices.COMException without installing Excel? 为什么使用此代码会得到“ InteropServices.COMException / ForwardCallToInvokeMember”? - Why would I get, “InteropServices.COMException / ForwardCallToInvokeMember” with this code? InteropServices.COMException(0x800A1066):在Teamcity下命令失败 - InteropServices.COMException (0x800A1066): Command failed under Teamcity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM