简体   繁体   中英

Windows forms, reflection and await

I have a Winforms app (.Net Core 3.0) that needs to use a binary assembly created for .Net Core 2.1

The binary assembly is loaded through reflection.

Some objects are instantiated (also through reflection) from that loaded assembly and some dependency objects local to the WinForms app are passed into those constructors at instantiation.

When those dependency objects are used within the Winforms application everything works as expected. There are async methods that can be awaited successfully.

However, when the 'reflectively created' objects have a method called on them that use the passed in dependency objects things work as expected up until the point that an await is called.

The result of the await succeeds (for example, a call to Cosmos Db where I can see the data changing) but the thread never returns from the await call.The forms main thread then picks up and moves on. If a few calls are made then the main thread (and the form) freezes.

These dependency objects do not interact with any Windows Forms controls.

If anyone has any ideas as to why the thread returns to the calling method and doesn't await I would be eternally grateful.

This was the line of code that was causing the issue (I got lost in the complexities of the project as a whole, unfortunately)

kvp.Value.ForEach(async handler => await ((dynamic)handler).Handle(@event, cancellationToken));

I have now added the tasks to be awaited to a List<Task> and await the WhenAll .

Doing it the way I had done it above meant that the enclosing method was not marked as async, as the expression was instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM