1

I'm calling an async method which is located in App.xaml.cs file. I'm calling this method from my c# file of home page (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))' This exception is thrown on the line starting with TEXT1_STRING.

The UpdateText() method is:

private async void UpdateText()
{
    HomePageViewText1.Text = "error text";

    TEXT1_STRING = Task.Run(() => App.GetJSONAsync()).Result;
    
    return;
}

GetJSONAsync() method code can be found below:

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#. Thank you.

cptalpdeniz
  • 368
  • 1
  • 11

2 Answers2

0

I've solved this exception by applying @M.M's solution. What I did was to return TEXT1_STRING = Task.Run(() => App.GetJSONAsync()).Result; to 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():

private async void UpdateText()
{
    HomePageViewText1.Text = "error text";

    TEXT1_STRING = await App.GetJSONAync();
    
    return;
}
cptalpdeniz
  • 368
  • 1
  • 11
0

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 . 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.

See also async/await - when to return a Task vs void? -- exception handling is different for async void functions than async Task functions.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • if I use this, I get: `The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'. ` – cptalpdeniz Dec 20 '22 at 01:19
  • if I apply your solution I get deadlock – cptalpdeniz Dec 20 '22 at 01:24
  • @cptalpdeniz you said a moment ago that this fixed the problem; and the error message you post suggests the method was not actually marked `async` as you have shown in the question? – M.M Dec 20 '22 at 01:41
  • The code only works if I leave UpdateText() function header as `private async void UpdateText()`. If I change it to `Task` then deadlock occurs. – cptalpdeniz Dec 20 '22 at 05:52
  • @cptalpdeniz OK, well glad to hear it is working although I think the deadlock on returning `Task` might be suggestive of further issues that you will have to address sooner or later – M.M Dec 20 '22 at 23:15
  • I see, since I don't understand these topics a lot, I'm working on learning more and hopefully I'll see where the error is in the future is and maybe revisit this. @M.M – cptalpdeniz Dec 21 '22 at 03:31