How do Asynchronous Procedure Calls handle marshaled delegates when you P/Invoke from C#?

后端 未结 2 1482
野趣味
野趣味 2021-01-06 11:30

I am wondering if it is possible to fall victim to issues around the management of managed threads in the native world when you marshal a callback delegate to a DLL through

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-06 12:01

    Asynchronous procedure calls exist completely on the native side. APC's know nothing of managed threads nor of marshaling. NotifyServiceStatusChange would have to call (or use the equivalent of) QueueUserAPC to dispatch the APC, which only takes an native thread handle. So, the APC will be queued to the native thread that calls NotifyServiceStatusChange.

    So this APC being queued and dispatched correctly rely on two things:

    1. The CLR keeps the native thread that it called NotifyServiceStatusChange.
    2. The CLR puts that native thread into an alterable wait.

    You control neither of these two things.

提交回复
热议问题