Juce Application : Multi threaded GUI application?


#1

Hi all,
I am working in an application in which I have the following requirements :

  1. I want to create multiple windows in the same applications which can play the quicktime movies simultaneously . I tried this by creating multiple DocumentWindows with quicktime components, but when I tried playing the 2 videos simultaneously it was playing in a choppy manner. I tried to find the reason and I guess the reason could be that Juce application is a single threaded UI application. Is it possible to make it a multi threaded GUI Application. If yes then what would be amount of work in it.
  2. My second requirement is that I want to exchange the messages between the message queues of two different UI processes . Is it possible doing this in Juce framework.

I would really appreciate any directions to these problems.
Thanks & Regards,
Surender.


#2

No, you can’t write a multi-threaded UI! All OSes and frameworks work with one message queue per process, and any attempt to have multiple threads dispatching messages to different windows would inevitably end up in chaos!

But I think you’re probably wrong in assuming it’d help anyway, even if it was possible. Quicktime will probably already be using background threads, and there shouldn’t be a bottleneck in the message queue, (unless your own code is blocking or flooding the message queue somehow)

As for sending messages between two processes - the InterprocessConnection class does a good job of posting messages between processes, but obviously you can only send blocks of data, not objects, so you’d need to serialise and deserialise your own messages at either end.


#3

Thanks for an instant reply Jules. I really appreciate it.

I was actually going through a msdn document on message queues: http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927(v=vs.85).aspx

where I found these sections :

[color=#0040BF]"Queued Messages
The system can display any number of windows at a time. To route mouse and keyboard input to the appropriate window, the system uses message queues.
The system maintains a single system message queue and one thread-specific message queue for each GUI thread. "

“Queued Messages
Whenever the user moves the mouse, clicks the mouse buttons, or types on the keyboard, the device driver for the mouse or keyboard converts the input into messages and places them in the system message queue. The system removes the messages, one at a time, from the system message queue, examines them to determine the destination window, and then posts them to the message queue of the thread that created the destination window. A thread’s message queue receives all mouse and keyboard messages for the windows created by the thread. The thread removes messages from its queue and directs the system to send them to the appropriate window procedure for processing.”

“Message Handling
An application must remove and process messages posted to the message queues of its threads. A single-threaded application usually uses a message loop in its WinMain function to remove and send messages to the appropriate window procedures for processing. Applications with multiple threads can include a message loop in each thread that creates a window.”
[/color]

Reading this I thought it is possible to have a multi-threaded GUI application. Can you please correct me if I am wrong in my understanding of the topic.

Thanks & Regards,
Surender.


#4

Well, yes, in win32 you can technically create windows that use their own separate message thread, but nobody does it, as it’d be very hard to work with.


#5

+1

Attempting to multithread the GUI is a fool’s errand. This having been said, there are some use cases for doing background processing of computationally expensive GUI related tasks, such as rendering a waveform.


#6

“Attempting to multithread the GUI is a fool’s errand. This having been said, there are some use cases for doing background processing of computationally expensive GUI related tasks, such as rendering a waveform.”

As stated in my original post I do have some specific requirements where I need to render the videos on multiple windows.

“But I think you’re probably wrong in assuming it’d help anyway, even if it was possible. Quicktime will probably already be using background threads, and there shouldn’t be a bottleneck in the message queue, (unless your own code is blocking or flooding the message queue somehow)”

I will try to go into the code and figure out why exactly is this happening, because I even used the juce demo with the quicktime component without anything extra in the code it still gives the same problem.

Peace.


#7

All operating systems that I’ve used do special / magical things when you play a video. Possibly decoding some of the data using the video card. Or at the very least, bypassing the standard window system / clipping rectangle and rendering directly to the frame buffer for speed. Having 2 videos going might force you system to switch to a “software” renderer, or give them different priorities depending on which one is in front.


#8

Just want to make something clear in case any n00bs are reading this thread…

I’ve been talking specifically in this topic about the idea of running multiple threads where each thread controls some GUI components. That is indeed, like Vinnie so eloquently said, a fool’s errand.

But don’t get this confused with having a GUI app that uses multiple threads, which is a totally normal thing to do. The difference being that you have one main message thread which handles all the GUI interaction and events, and other background threads which handle background processing tasks that would otherwise block the main thread.


#9

Thanks for directions Jules and Vinn, appreciate the guidance.


#10

I’ve seen some systems that dedicates a thread to GUI processing/rendering.
Main thread runs message loop, and GUI thread handles drawing and animations.

Cascades (blackberry 10 GUI framework) runs this way to enable fluid animations.
https://developer.blackberry.com/cascades/documentation/getting_started/introtocascades.html

I found this interesting :slight_smile: (but not realy related to this topic)


#11

I use vlclib here for video decoding, it’s quite easy to setup, and really efficient (i already tested to decode several videos simultaneously without probs)
Maybe you could try that in place of quicktime component?