To me, it sounds like your problem is more of a symptom of a bigger issue. I personally try to avoid as much as I can putting business logic in a C++-CLI module. Specifically, I try to limit a ref class
functionality to 3 main areas:
- Translating managed method calls to native method calls (that includes transforming parameters if necessary).
- Translating native return values to managed return values. This can even be translating an asynchronous function receiving a callback to a method returning
Task
.
- Translating native events (callbacks) to managed events.
I Might be wrong, but in your situation it sounds like your C++ code is not well decoupled and you end up wiring different parts of it inside a C++-CLI module.
As for your question, I would use Task.ContinueWith
family of methods to perform the asynchronous continuation instead of async / await
. Yet, If you want the continuation to be invoked on a specific SynchronizationContext
(such as UI thread) then special care must be taken to supply the right TaskScheduler
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…