Skip to content

STA-Thread lost after async operation #5729

@SamuelMuellerKMS

Description

@SamuelMuellerKMS

Describe the bug

When running a STATestMethod and calling a async Method, it's not syncing back to the STA Thread

Steps To Reproduce

Execute an async method in a STATestMethod. After the Execution you will not return on the STA-Thread.

[STATestMethod]
public async Task StaTestMethodIssueTest()
{
	Console.WriteLine($"Current Thread Apartment State: {Thread.CurrentThread.GetApartmentState()}");

	Console.WriteLine("Executing async Method...");
	await Task.Yield();
		
	Console.WriteLine($"Current Thread Apartment State: {Thread.CurrentThread.GetApartmentState()}");
	Assert.IsTrue(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA, "Thread is not in Single Threaded Apartment (STA) mode");
}

Example Project: STATestIssue.zip

Expected behavior

After executing an async Method you should return to the calling thread (or not when .ConfigureAwait(false) is present)

Actual behavior

The STA Thread Context is lost and you get Exceptions when calling code that requires an STA-Thread

Metadata

Metadata

Assignees

Labels

Area: MSTestIssues with MSTest that are not specific to more refined area (e.g. analyzers or assertions)Needs: DesignNeeds research and proposal for how the feature will be implemented.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions