MFC data forwarding to main thread via PostMessage

前端 未结 3 2030
时光取名叫无心
时光取名叫无心 2021-02-10 03:08

I have a C++/MFC application I need to restructure. The app used to process most of the data on the main thread, therefore blocking the input, and now I want to change it so, th

3条回答
  •  猫巷女王i
    2021-02-10 03:59

    I solved a similar problem some time ago. I made a singleton queue to hold the data (or actions) that need to flow from background threads to the UI (main) thread. The queue is protected by a critical section. Background thread would place its data in the queue and post a message. The message does not hold any data, it acts as a simple wake-up call "hey, main thread, look at the queue, there's work for you".

    This way, you don't risk leaking any memory or other resources; the queue can be safely destroyed with all the data it contains.

提交回复
热议问题