Skip to content

Integration with existing GUI thread #39

@derceg

Description

@derceg

Hi, I'm looking at integrating your library into my application, since the functionality it offers would be very helpful when it comes to writing asynchronous code. The thing I'd find most useful would be to be able to call PostTaskAndReply from the existing GUI thread and have that work. Out of the box, it doesn't work, because as your documentation notes, that method has to be called from a thread with a task queue.

I can see a potential way I could work around that:

  • Create a custom MessagePump. When QueuePendingTask is called, the pump would invoke PostMessage to notify the main thread, via the standard Windows message loop, that a task is available.
  • Create a class analogous to the existing Thread class (something like MainThread) that holds a SingleThreadTaskRunner. That class would run any pending tasks once notified (via the PostMessage call).
  • That class would also set up the ScopedSequenceIdSetter and SequencedTaskRunnerHandle instances for the main thread once at startup, to ensure that there's always a SequencedTaskRunnerHandle available on the main thread.

I'm hoping to get your feedback on whether the last point is reasonable. My understanding is that if the main thread were solely processing tasks (and message loop items were turned into tasks), things would work, but that's a larger change that I don't really want to go through with. And it doesn't look like this library is set up to support that anyway (e.g. the Thread class only processes tasks and has no handling for something like the Windows message loop).

The solution above also requires using a few things under base::detail - for example, base::detail::SequenceIdGenerator and base::detail::ScopedSequenceIdSetter, so I'm wondering whether that could be better supported in the library.

Even without the changes above, I'd still need to be able to either customize what the thread message loop is doing (e.g. a Windows COM thread can need to pump the windows message loop) or create a custom class that does what I need. In which case, I'd still need to refer to base::detail::SequenceIdGenerator to construct the task runner.

Thanks for any help.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions