C# Multithreading — Invoke without a Control

前端 未结 7 1176
闹比i
闹比i 2020-12-13 20:44

I am only somewhat familiar with multi-threading in that I\'ve read about it but have never used it in practice.

I have a project that uses a third party library tha

7条回答
  •  爱一瞬间的悲伤
    2020-12-13 21:17

    Is there a way around this?

    Yes, the work-around would be for you to create a thread-safe queue.

    • Your event handler is invoked by the 3rd-party thread
    • Your event handler enqueues something (event data) onto a collection (e.g. a List) which you own
    • Your event handler does something to signal your own thead, that there's data in the collection for it to dequeue and process:
      • Your thread could be waiting on something (a mutex or whatever); when its mutex is signalled by the event handler, it wakes up and checks the queue.
      • Alternatively, instead of being signalled, it could wake up periodically (e.g. once per second or whatever) and poll the queue.

    In either case, because your queue is being written by two different threads (the 3rd-party thread is enqueueing, and your thread is dequeueing), it needs to be a thread-safe, protected queue.

提交回复
热议问题