1

I ran into a situation like this:

    private async Task SomeMethod(Task<SomeType> dataTask)
    {
        var data = await dataTask;
       
        // process data...
    }

We have the VS-threading analyzer and this emit a warning: VSTHRD003 Avoid awaiting foreign Tasks

The solution is to somehow use a JoinableTaskFactory, but I can't figure out a way to make this work with this situation. I understand that dataTask is running on a different context than in this method, but I'm not sure how a JTF is supposed to solve this. Should I create my own context to run the task and then pass this to SomeMethod() to actually run it?

I have access to the calling method, so I can make changes there. Neither methods need the main thread so I guess I can ignore this warning, but I still want to know how to properly solve this warning.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
LeCholet
  • 33
  • 7

1 Answers1

0

This is not a knowledgeable and satisfying answer to your question, but it might still be useful to help you move forward. The JoinableTaskFactory is an esoteric type of the Microsoft.VisualStudio.Threading package, that very few people are familiar with, and even less have ever used. You might try to understand why it exists and what it does by reading posts by Andrew Arnott, like this post. My personal suggestion is to ignore all rules that are included in this package, unless you are writing extensions for the Visual Studio, in which case you might have to familiarize yourself with this type.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104