WDFWORKITEM automatic deletion vs notifying EvtWorkItemFunc

I have an interesting dilemma with WDFWORKITEM. A work item has to be created with a parent object being a WDFDEVICE. Otherwise WdfWorkItemCreate fails.

So when the driver begins unloading, since the work-item is a child of its device, the framework first waits for the EvtWorkItemFunc of my WDFWORKITEM to finish before invoking EvtCleanupCallback on the said device. And because of that my EvtWorkItemFunc takes its sweet time not realizing that the driver is unloading, which I would prefer to fix.

But I can't seem to find a way to get notified if the device is being cleaned up, or that the driver is being unloaded before the framework starts waiting for my EvtWorkItemFunc to finish. A classic catch-22.

Alternatively I can't create a WDFWORKITEM with a NULL parent and do all the reference counting manually.

Any suggestions how to resolve this?

If the work item is waiting on an event, wait on both the original event and a new device cleanup event. Signal that event during device cleanup.

Sorry, what?

The device cleanup is not invoked before the framework starts waiting on my EvtWorkItemFunc to finish. That is the problem, i.e. where do I signal my event from to notify EvtWorkItemFunc to quit?

But I think I see my problem here. The WDFWORKITEM is a good concept and also quite easy to use. But it is absolutely not designed to wait. It is great to quickly process its EvtWorkItemFunc without any waiting.

All in all, I abandoned WDFWORKITEM for my purpose and will roll my own using WDM and system threads. I wasted too much time yesterday trying to shoehorn it.