Skip to content

JoinableTaskContext.IsOnMainThread should satisfy VSTHRD010 #1100

Description

@zivkan

Is your feature request related to a problem? Please describe.

Previously commented in another issue: #272 (comment)

I have a method I want to be free-threaded, but I have dependencies which are not. How I implement my method depends whether it's running on the main thread, or a background thread. Copying the example from the other issue:

public void NotAffinitized() {
  if (IsOnMainThread()) {
    // This incorrectly marks the enclosing method as affinitized, which then propagates
    SomeAffinitizedMethod();
  } else {
    SomeNonAffinitizedMethod();
  }
}

Describe the solution you'd like

No VSTHRD010 within the if block.

Describe alternatives you've considered

An await jtf.SwitchToMainThreadAsync(alwaysYield: false) does suppress the warning, but feels inefficient. Even if it's fast, it's still extra work that isn't necessary for program correctness, just to satisfy the analyzer.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions