www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multi-threaded GUI

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d04088ee591bd7c04c5abba64
Content-Type: text/plain; charset=UTF-8

Hi!

I'm trying to write a WinAPI example to have multi-threaded GUI. I wanna
have a Window class, which creates a window and listens to its messages in
a separate thread when constructed.  This will allow me to write a main
function like this:

void main()
{
    Window w = new Window;
    w.move(100, 200);
    w.resize(800, 600);
    w.show();
}

The methods called for the window will send asynchronous messages, which
will cause the window to change its position, size and visibility
on-the-fly. This is convenient, because no message loop needs to be
launched separately and every window will rocess its messages in a separate
thread.

Can anyone please tell me how to achieve this?

-- 
Bye,
Gor Gyolchanyan.

--f46d04088ee591bd7c04c5abba64
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Hi!<div><br></div><div>I&#39;m trying to write a WinAPI example to have mul=
ti-threaded GUI. I wanna have a Window class, which creates a window and li=
stens to its messages in a separate thread when constructed. =C2=A0This wil=
l allow me to write a main function like this:</div>
<div><br></div><div>void main()</div><div>{</div><div>=C2=A0 =C2=A0 Window =
w =3D new Window;</div><div>=C2=A0 =C2=A0 w.move(100, 200);</div><div>=C2=
=A0 =C2=A0 w.resize(800, 600);</div><div>=C2=A0 =C2=A0 w.show();</div><div>=
}</div><div><br></div><div>The methods called for the window will send asyn=
chronous messages, which will cause the window to change its position, size=
 and visibility on-the-fly. This is convenient, because no message loop nee=
ds to be launched separately and every window will rocess its messages in a=
 separate thread.</div>
<div><br></div><div>Can anyone please tell me how to achieve this?</div><di=
v><div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br>
</div>

--f46d04088ee591bd7c04c5abba64--
Jul 25 2012
next sibling parent reply Simon <s.d.hammett gmail.com> writes:
On 25/07/2012 19:34, Gor Gyolchanyan wrote:
 Hi!

 I'm trying to write a WinAPI example to have multi-threaded GUI. I wanna
 have a Window class, which creates a window and listens to its messages
 in a separate thread when constructed.  This will allow me to write a
 main function like this:

 void main()
 {
      Window w = new Window;
      w.move(100, 200);
      w.resize(800, 600);
      w.show();
 }

 The methods called for the window will send asynchronous messages, which
 will cause the window to change its position, size and visibility
 on-the-fly. This is convenient, because no message loop needs to be
 launched separately and every window will rocess its messages in a
 separate thread.

 Can anyone please tell me how to achieve this?

 --
 Bye,
 Gor Gyolchanyan.

It depends exactly on what you are trying to do, but in general: You have to be very, very careful with trying to do multi threading w/ windoze windows. Try doing a google search on it, and the advice is invariably: don't do multi threaded windows. Everybody including people famous for their in-depth window knowledge recommends a single thread UI with non-UI worker threads. Having completely separate top level windows each in it's own thread is ok, but if you want to have a parent/child relation between windows in different threads, then you can not use any thread synchronisation primitives all at other than MsgWaitForMultipleObjectsEx, otherwise you will have a guaranteed deadlock. In which case you'd have to do all of the threading your self and not use anything in phobos. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jul 25 2012
next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-07-26 11:45, Gor Gyolchanyan wrote:

 So the good way to do this will be to have a single thread, which pumps
 messages and distributes the appropriate message handlers to worker
 threads, right?

It should be possible to have other threads interacting with the GUI as long as they do that by sending messages to the thread responsible to for GUI. Here's an example of an SWT snippet that does GUI manipulation from a different thread: http://git.eclipse.org/c/platform/eclipse.platform.swt.git/tree/examples/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet130.java On line 42 a new thread is created. On line 48 the GUI is changed, by adding a string to some text widget. But this happens using the "display.syncExec" method. This force the code to be executed on the GUI thread instead of the current thread. -- /Jacob Carlborg
Jul 26 2012
prev sibling parent reply Simon <s.d.hammett gmail.com> writes:
On 26/07/2012 09:16, Russel Winder wrote:
 On Wed, 2012-07-25 at 23:17 +0200, Kagamin wrote:
 On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:
 You have to be very, very careful with trying to do multi
 threading w/ windoze windows. Try doing a google search on it,
 and the advice is invariably: don't do multi threaded windows.
 Everybody including people famous for their in-depth window
 knowledge recommends a single thread UI with non-UI worker
 threads.


would be very interesting. Where is the material that I can look at that leads you to say that Windows is a multi-threaded UI.

On 'doze every thread can have it's own message pump and therefore it's own UI, but if you have an ancestor/descent relation between 2 windows they must belong to the same thread. Otherwise you are pretty much guaranteed a deadlock, unless you only ever use MsgWaitForMultipleObjectsEx to synchronise. But that's kinda of unachievable unless you have written every single line of code yourself. So you can have threads with separate UIs as long as you don't make the windows of one thread the child of another. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk
Jul 26 2012
parent KlausO <oberhofer users.sf.net> writes:
Am 26.07.2012 19:15, schrieb Simon:
 On 26/07/2012 09:16, Russel Winder wrote:
 On Wed, 2012-07-25 at 23:17 +0200, Kagamin wrote:
 On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:
 You have to be very, very careful with trying to do multi
 threading w/ windoze windows. Try doing a google search on it,
 and the advice is invariably: don't do multi threaded windows.
 Everybody including people famous for their in-depth window
 knowledge recommends a single thread UI with non-UI worker
 threads.


would be very interesting. Where is the material that I can look at that leads you to say that Windows is a multi-threaded UI.

On 'doze every thread can have it's own message pump and therefore it's own UI, but if you have an ancestor/descent relation between 2 windows they must belong to the same thread. Otherwise you are pretty much guaranteed a deadlock, unless you only ever use MsgWaitForMultipleObjectsEx to synchronise. But that's kinda of unachievable unless you have written every single line of code yourself. So you can have threads with separate UIs as long as you don't make the windows of one thread the child of another.

Every thread that creates a window will get a message queue created and assigned (It's then called a GUI thread). Under normal circumstances your thread will enter an event loop where you call the GetMessage/DispatchMessage function pair. GetMessage gets only messages from the threads message queue and DispatchMessage dispatches only to the windows owned by the thread. You can get into troubles when you send a message via SendMessage from thread A to a window owned by thread B and the event loop of thread B does not handle this message (e.g. by having a modal dialog opened in thread B). In this case thread A is blocked until the message is processed by B's event loop. SendMessage is often used in code dealing with Parent/Child window relations, so the warning above is justified. But as long as your child window event loop is not blocked (or some other access to a shared resource creates a deadlock) it works. There is a technical article about multithreaded GUI and Win32 on MSDN. See http://msdn.microsoft.com/en-us/library/ms810439.aspx
Jul 27 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--f46d04088ee53f58a004c5ac3aa5
Content-Type: text/plain; charset=UTF-8

On Wed, Jul 25, 2012 at 10:50 PM, Simon <s.d.hammett gmail.com> wrote:

 On 25/07/2012 19:34, Gor Gyolchanyan wrote:

 Hi!

 I'm trying to write a WinAPI example to have multi-threaded GUI. I wanna
 have a Window class, which creates a window and listens to its messages
 in a separate thread when constructed.  This will allow me to write a
 main function like this:

 void main()
 {
      Window w = new Window;
      w.move(100, 200);
      w.resize(800, 600);
      w.show();
 }

 The methods called for the window will send asynchronous messages, which
 will cause the window to change its position, size and visibility
 on-the-fly. This is convenient, because no message loop needs to be
 launched separately and every window will rocess its messages in a
 separate thread.

 Can anyone please tell me how to achieve this?

 --
 Bye,
 Gor Gyolchanyan.

It depends exactly on what you are trying to do, but in general: You have to be very, very careful with trying to do multi threading w/ windoze windows. Try doing a google search on it, and the advice is invariably: don't do multi threaded windows. Everybody including people famous for their in-depth window knowledge recommends a single thread UI with non-UI worker threads. Having completely separate top level windows each in it's own thread is ok, but if you want to have a parent/child relation between windows in different threads, then you can not use any thread synchronisation primitives all at other than MsgWaitForMultipleObjectsEx, otherwise you will have a guaranteed deadlock. In which case you'd have to do all of the threading your self and not use anything in phobos. -- My enormous talent is exceeded only by my outrageous laziness. http://www.ssTk.co.uk

-- Bye, Gor Gyolchanyan. --f46d04088ee53f58a004c5ac3aa5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Wed, Jul 25, 2012 at 10:50 PM, Simon <span di= r=3D"ltr">&lt;<a href=3D"mailto:s.d.hammett gmail.com" target=3D"_blank">s.= d.hammett gmail.com</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quo= te" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"=

n wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi!<br> <br> I&#39;m trying to write a WinAPI example to have multi-threaded GUI. I wann= a<br> have a Window class, which creates a window and listens to its messages<br> in a separate thread when constructed. =C2=A0This will allow me to write a<= br> main function like this:<br> <br> void main()<br> {<br> =C2=A0 =C2=A0 =C2=A0Window w =3D new Window;<br> =C2=A0 =C2=A0 =C2=A0w.move(100, 200);<br> =C2=A0 =C2=A0 =C2=A0w.resize(800, 600);<br> =C2=A0 =C2=A0 =C2=A0w.show();<br> }<br> <br> The methods called for the window will send asynchronous messages, which<br=

on-the-fly. This is convenient, because no message loop needs to be<br> launched separately and every window will rocess its messages in a<br> separate thread.<br> <br> Can anyone please tell me how to achieve this?<br> <br> --<br> Bye,<br> Gor Gyolchanyan.<br> </blockquote> <br></div></div> It depends exactly on what you are trying to do, but in general:<br> <br> You have to be very, very careful with trying to do multi threading w/ wind= oze windows. Try doing a google search on it, and the advice is invariably:= don&#39;t do multi threaded windows. Everybody including people famous for= their in-depth window knowledge recommends a single thread UI with non-UI = worker threads.<br> <br> Having completely separate top level windows each in it&#39;s own thread is= ok, but if you want to have a parent/child relation between windows in dif= ferent threads, then you can not use any thread synchronisation primitives = all at other than MsgWaitForMultipleObjectsEx, otherwise you will have a gu= aranteed deadlock. In which case you&#39;d have to do all of the threading = your self and not use anything in phobos.<span class=3D"HOEnZb"><font color= =3D"#888888"><br> <br> -- <br> My enormous talent is exceeded only by my outrageous laziness.<br> <a href=3D"http://www.ssTk.co.uk" target=3D"_blank">http://www.ssTk.co.uk</= a><br> <br> <br> </font></span></blockquote></div><br>I see. Thanks for the reply. Somehow I= suspected this to be the case.<br clear=3D"all"><div><br></div>-- <br>Bye,= <br>Gor Gyolchanyan.<br> --f46d04088ee53f58a004c5ac3aa5--
Jul 25 2012
prev sibling next sibling parent Dmitry Olshansky <dmitry.olsh gmail.com> writes:
On 25-Jul-12 22:34, Gor Gyolchanyan wrote:
 Hi!

 I'm trying to write a WinAPI example to have multi-threaded GUI. I wanna
 have a Window class, which creates a window and listens to its messages
 in a separate thread when constructed.  This will allow me to write a
 main function like this:

 void main()
 {
      Window w = new Window;
      w.move(100, 200);
      w.resize(800, 600);
      w.show();
 }

 The methods called for the window will send asynchronous messages, which
 will cause the window to change its position, size and visibility
 on-the-fly.

Use plain WinAPI SendMessage in all threads and one separate thread for GUI message pump. In fact you can change shape and visibility of any window in a system. And you do not need to be superuser. Hence proliferation of locker type Trojans. Who told Windows is boring? ;)
This is convenient, because no message loop needs to be
 launched separately and every window will rocess its messages in a
 separate thread.

 Can anyone please tell me how to achieve this?

pump per application. Hence problematic separation of window per thread. Again that most straightforward (and effective) design is to use 1 thread for all UI and a bunch of workers for other stuff and you are good to go. Or, in your model, it looks like GUI is a background process, and workers rule it, interesting view esp w.r.t. user perspective :) -- Dmitry Olshansky
Jul 25 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--e0cb4efe33906a1f1604c5ac6e7b
Content-Type: text/plain; charset=UTF-8

On Wed, Jul 25, 2012 at 11:18 PM, Dmitry Olshansky <dmitry.olsh gmail.com>wrote:

 On 25-Jul-12 22:34, Gor Gyolchanyan wrote:

 Hi!

 I'm trying to write a WinAPI example to have multi-threaded GUI. I wanna
 have a Window class, which creates a window and listens to its messages
 in a separate thread when constructed.  This will allow me to write a
 main function like this:

 void main()
 {
      Window w = new Window;
      w.move(100, 200);
      w.resize(800, 600);
      w.show();
 }

 The methods called for the window will send asynchronous messages, which
 will cause the window to change its position, size and visibility
 on-the-fly.

Use plain WinAPI SendMessage in all threads and one separate thread for GUI message pump. In fact you can change shape and visibility of any window in a system. And you do not need to be superuser. Hence proliferation of locker type Trojans. Who told Windows is boring? ;) This is convenient, because no message loop needs to be
 launched separately and every window will rocess its messages in a
 separate thread.

 Can anyone please tell me how to achieve this?

  The problem is that WinAPI event loop was created with single message

Again that most straightforward (and effective) design is to use 1 thread for all UI and a bunch of workers for other stuff and you are good to go. Or, in your model, it looks like GUI is a background process, and workers rule it, interesting view esp w.r.t. user perspective :) -- Dmitry Olshansky

Yes. The idea was to programmatically operate on UI objects declaratively. For instance, create a window, change its size, show it, draw on it and never worry about "applying" (in the form of pumping the messages). I guess the same effect can be achieved by having all windows created in a separate thread and the message ump called in static ~this(); -- Bye, Gor Gyolchanyan. --e0cb4efe33906a1f1604c5ac6e7b Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Wed, Jul 25, 2012 at 11:18 PM, Dmitry Olshans= ky <span dir=3D"ltr">&lt;<a href=3D"mailto:dmitry.olsh gmail.com" target=3D= "_blank">dmitry.olsh gmail.com</a>&gt;</span> wrote:<br><blockquote class= =3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"> <div class=3D"im">On 25-Jul-12 22:34, Gor Gyolchanyan wrote:<br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> Hi!<br> <br> I&#39;m trying to write a WinAPI example to have multi-threaded GUI. I wann= a<br> have a Window class, which creates a window and listens to its messages<br> in a separate thread when constructed. =C2=A0This will allow me to write a<= br> main function like this:<br> <br> void main()<br> {<br> =C2=A0 =C2=A0 =C2=A0Window w =3D new Window;<br> =C2=A0 =C2=A0 =C2=A0w.move(100, 200);<br> =C2=A0 =C2=A0 =C2=A0w.resize(800, 600);<br> =C2=A0 =C2=A0 =C2=A0w.show();<br> }<br> <br> The methods called for the window will send asynchronous messages, which<br=

on-the-fly.<br> </blockquote> <br></div> Use plain WinAPI SendMessage in all threads and one separate thread for GUI= message pump.<br> In fact you can change shape and visibility of any window in a system. And = you do not need to be superuser. Hence proliferation of locker type Trojans= . Who told Windows is boring? ;)<div class=3D"im"><br> <br> <blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p= x #ccc solid;padding-left:1ex"> This is convenient, because no message loop needs to be<br> launched separately and every window will rocess its messages in a<br> separate thread.<br> <br> Can anyone please tell me how to achieve this?<br> <br> </blockquote></div> The problem is that WinAPI event loop was created with single message pump = per application. Hence problematic separation of window per thread.<br> <br> Again that most straightforward (and effective) design is to use 1 thread f= or all UI and a bunch of workers for other stuff and you are good to go.<br=

Or, in your model, it looks like GUI is a background process, and workers r= ule it, interesting view esp w.r.t. user perspective :)<span class=3D"HOEnZ= b"><font color=3D"#888888"><br> <br> -- <br> Dmitry Olshansky<br> </font></span></blockquote></div><br>Yes. The idea was to programmatically = operate on UI objects declaratively. For instance, create a window, change = its size, show it, draw on it and never worry about &quot;applying&quot; (i= n the form of pumping the messages).<br clear=3D"all"> <div><br></div><div>I guess the same effect can be achieved by having all w= indows created in a separate thread and the message ump called in static ~t= his();</div><div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br> --e0cb4efe33906a1f1604c5ac6e7b--
Jul 25 2012
prev sibling next sibling parent reply "Kagamin" <spam here.lot> writes:
On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:
 You have to be very, very careful with trying to do multi 
 threading w/ windoze windows. Try doing a google search on it, 
 and the advice is invariably: don't do multi threaded windows. 
 Everybody including people famous for their in-depth window 
 knowledge recommends a single thread UI with non-UI worker 
 threads.

Hmm... AFAIK it's quite opposite: windows UI is the only UI that works in multithreaded environment, it's just recommended to not do it, because this feature is inconsistent with other UI frameworks, so that you don't get used to wrong programming technique.
Jul 25 2012
parent "Paulo Pinto" <pjmlp progtools.org> writes:
"Russel Winder"  wrote in message 
news:mailman.675.1343290603.31962.digitalmars-d puremagic.com...
 So if Windows is really doing multi-threaded widget management, this
would be very interesting.  Where is the material that I can look at
that leads you to say that Windows is a multi-threaded UI.

At the moment I can only post this, as I am at work and don't have much time to search for info. [quote]The system maintains a single system message queue and one thread-specific message queue for each GUI thread.[/quote] http://msdn.microsoft.com/en-us/library/windows/desktop/ms644927%28v=vs.85%29.aspx -- Paulo
Jul 26 2012
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Wednesday, 25 July 2012 at 19:25:09 UTC, Gor Gyolchanyan wrote:
 Yes. The idea was to programmatically operate on UI objects 
 declaratively.
 For instance, create a window, change its size, show it, draw 
 on it and
 never worry about "applying" (in the form of pumping the 
 messages).

Haha, show window and return from main.
Jul 25 2012
prev sibling next sibling parent Russel Winder <russel winder.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Wed, 2012-07-25 at 23:17 +0200, Kagamin wrote:
 On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:
 You have to be very, very careful with trying to do multi=20
 threading w/ windoze windows. Try doing a google search on it,=20
 and the advice is invariably: don't do multi threaded windows.=20
 Everybody including people famous for their in-depth window=20
 knowledge recommends a single thread UI with non-UI worker=20
 threads.

Hmm... AFAIK it's quite opposite: windows UI is the only UI that=20 works in multithreaded environment, it's just recommended to not=20 do it, because this feature is inconsistent with other UI=20 frameworks, so that you don't get used to wrong programming=20 technique.

I know essentially nothing of Windows, but quite a lot about AWT/Swing/JavaFX/GroovyFX, GTK+2, GTK+3, PyGTK, PyGObject, PySide, PyQt4, wxPython, and tkInter. As you say all of these use an single-threaded event loop to manage all widgets. Fundamentally this is because there is only one screen and one window manager, and all widgets need to be manipulated relative to that. Using a single-threaded event loop is the easiest way of avoid deadlock and livelock given that there is a shared resource. The same message has been learnt in Web servers where the multi-threaded approach has given way to a single-threaded event loop approach. So if Windows is really doing multi-threaded widget management, this would be very interesting. Where is the material that I can look at that leads you to say that Windows is a multi-threaded UI. As Simon mention, just because the UI is single-threaded doesn't mean the application is. Exactly the opposite, good UI-based applications are generally multi-threaded, it is just that there is only one thread running the UI, all the others handle business logic. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jul 26 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--047d7b33d9da7095b004c5b875de
Content-Type: text/plain; charset=UTF-8

On Thu, Jul 26, 2012 at 12:16 PM, Russel Winder <russel winder.org.uk>wrote:

 On Wed, 2012-07-25 at 23:17 +0200, Kagamin wrote:
 On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:
 You have to be very, very careful with trying to do multi
 threading w/ windoze windows. Try doing a google search on it,
 and the advice is invariably: don't do multi threaded windows.
 Everybody including people famous for their in-depth window
 knowledge recommends a single thread UI with non-UI worker
 threads.

Hmm... AFAIK it's quite opposite: windows UI is the only UI that works in multithreaded environment, it's just recommended to not do it, because this feature is inconsistent with other UI frameworks, so that you don't get used to wrong programming technique.

I know essentially nothing of Windows, but quite a lot about AWT/Swing/JavaFX/GroovyFX, GTK+2, GTK+3, PyGTK, PyGObject, PySide, PyQt4, wxPython, and tkInter. As you say all of these use an single-threaded event loop to manage all widgets. Fundamentally this is because there is only one screen and one window manager, and all widgets need to be manipulated relative to that. Using a single-threaded event loop is the easiest way of avoid deadlock and livelock given that there is a shared resource. The same message has been learnt in Web servers where the multi-threaded approach has given way to a single-threaded event loop approach. So if Windows is really doing multi-threaded widget management, this would be very interesting. Where is the material that I can look at that leads you to say that Windows is a multi-threaded UI. As Simon mention, just because the UI is single-threaded doesn't mean the application is. Exactly the opposite, good UI-based applications are generally multi-threaded, it is just that there is only one thread running the UI, all the others handle business logic. -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder

So the good way to do this will be to have a single thread, which pumps messages and distributes the appropriate message handlers to worker threads, right? My goal here is to have 100% working and responsive GUI no matter how bad the application lags. If, for instance, The user would push a button, which initiates a very expensive computation, I don't want the GUI to become stuck. If the user doesn't wait and pushes the button again, it should display a message, which says "The operation is already in progress" or something like that. -- Bye, Gor Gyolchanyan. --047d7b33d9da7095b004c5b875de Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Thu, Jul 26, 2012 at 12:16 PM, Russel Winder = <span dir=3D"ltr">&lt;<a href=3D"mailto:russel winder.org.uk" target=3D"_bl= ank">russel winder.org.uk</a>&gt;</span> wrote:<br><blockquote class=3D"gma= il_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-lef= t:1ex"> <div class=3D"HOEnZb"><div class=3D"h5">On Wed, 2012-07-25 at 23:17 +0200, = Kagamin wrote:<br> &gt; On Wednesday, 25 July 2012 at 18:50:51 UTC, Simon wrote:<br> &gt; &gt; You have to be very, very careful with trying to do multi<br> &gt; &gt; threading w/ windoze windows. Try doing a google search on it,<br=

.<br> &gt; &gt; Everybody including people famous for their in-depth window<br> &gt; &gt; knowledge recommends a single thread UI with non-UI worker<br> &gt; &gt; threads.<br> &gt;<br> &gt; Hmm... AFAIK it&#39;s quite opposite: windows UI is the only UI that<b= r> &gt; works in multithreaded environment, it&#39;s just recommended to not<b= r> &gt; do it, because this feature is inconsistent with other UI<br> &gt; frameworks, so that you don&#39;t get used to wrong programming<br> &gt; technique.<br> <br> </div></div>I know essentially nothing of Windows, but quite a lot about<br=

PyQt4, wxPython, and tkInter. As you say all of these use an<br> single-threaded event loop to manage all widgets. Fundamentally this is<br> because there is only one screen and one window manager, and all widgets<br=

loop is the easiest way of avoid deadlock and livelock given that there<br> is a shared resource.<br> <br> The same message has been learnt in Web servers where the multi-threaded<br=

<br> So if Windows is really doing multi-threaded widget management, this<br> would be very interesting. =C2=A0Where is the material that I can look at<b= r> that leads you to say that Windows is a multi-threaded UI.<br> <br> As Simon mention, just because the UI is single-threaded doesn&#39;t mean<b= r> the application is. Exactly the opposite, good UI-based applications are<br=

running the UI, all the others handle business logic.<br> <span class=3D"HOEnZb"><font color=3D"#888888">--<br> Russel.<br> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D<br> Dr Russel Winder =C2=A0 =C2=A0 =C2=A0t: +44 20 7585 2200 =C2=A0 voip: <a hr= ef=3D"mailto:sip%3Arussel.winder ekiga.net">sip:russel.winder ekiga.net</a>= <br> 41 Buckmaster Road =C2=A0 =C2=A0m: +44 7770 465 077 =C2=A0 xmpp: <a href=3D= "mailto:russel winder.org.uk">russel winder.org.uk</a><br> London SW11 1EN, UK =C2=A0 w: <a href=3D"http://www.russel.org.uk" target= =3D"_blank">www.russel.org.uk</a> =C2=A0skype: russel_winder<br> </font></span></blockquote></div><br>So the good way to do this will be to = have a single thread, which pumps messages and distributes the appropriate = message handlers to worker threads, right?<div><br></div><div>My goal here = is to have 100% working and responsive GUI no matter how bad the applicatio= n lags. If, for instance, The user would push a button, which initiates a v= ery expensive computation, I don&#39;t want the GUI to become stuck. If the= user doesn&#39;t wait and pushes the button again, it should display a mes= sage, which says &quot;The operation is already in progress&quot; or someth= ing like that.<br clear=3D"all"> <div><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br> </div> --047d7b33d9da7095b004c5b875de--
Jul 26 2012
prev sibling next sibling parent Russel Winder <russel winder.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, 2012-07-26 at 13:45 +0400, Gor Gyolchanyan wrote:

 So the good way to do this will be to have a single thread, which pumps
 messages and distributes the appropriate message handlers to worker
 threads, right?
=20
 My goal here is to have 100% working and responsive GUI no matter how bad
 the application lags. If, for instance, The user would push a button, whi=

 initiates a very expensive computation, I don't want the GUI to become
 stuck. If the user doesn't wait and pushes the button again, it should
 display a message, which says "The operation is already in progress" or
 something like that.

Your goal is excellent. Throughout the early 2000s my staff had to suffer the stupidities of software development environments that failed to take this message on. The "standard model" for doing this is for user event callbacks to always act and if they need to do something that takes longer than a very few instructions and/or takes more that a fraction of second (NB user reaction time is around 0.2s, user boredom time is around 2s) spawn a thread to undertake the work. If that then involves interaction, the thread puts an event on the event queue and terminates. Separation of concerns is also important here: no business logic in the GUI code, no GUI code in the business logic. MVC (*), Mediator, Fa=C3=A7ade= , are your friends. (*) Microsoft's MVP variant of MVC is probably very appropriate on Windows. --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jul 26 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
--047d7b603ebc40bed904c5b91f0c
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Thu, Jul 26, 2012 at 2:15 PM, Russel Winder <russel winder.org.uk> wrote=
:

 On Thu, 2012-07-26 at 13:45 +0400, Gor Gyolchanyan wrote:

 So the good way to do this will be to have a single thread, which pumps
 messages and distributes the appropriate message handlers to worker
 threads, right?

 My goal here is to have 100% working and responsive GUI no matter how b=


 the application lags. If, for instance, The user would push a button,

 initiates a very expensive computation, I don't want the GUI to become
 stuck. If the user doesn't wait and pushes the button again, it should
 display a message, which says "The operation is already in progress" or
 something like that.

Your goal is excellent. Throughout the early 2000s my staff had to suffer the stupidities of software development environments that failed to take this message on. The "standard model" for doing this is for user event callbacks to always act and if they need to do something that takes longer than a very few instructions and/or takes more that a fraction of second (NB user reaction time is around 0.2s, user boredom time is around 2s) spawn a thread to undertake the work. If that then involves interaction, the thread puts an event on the event queue and terminates. Separation of concerns is also important here: no business logic in the GUI code, no GUI code in the business logic. MVC (*), Mediator, Fa=C3=A7a=

 are your friends.

 (*) Microsoft's MVP variant of MVC is probably very appropriate on
 Windows.
 --
 Russel.

 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D
 Dr Russel Winder      t: +44 20 7585 2200   voip:
 sip:russel.winder ekiga.net
 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel winder.org.uk
 London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder

I totally agree with separation of GUI and logic. The problem is, that the engine should be generic, so there's no way of determining whether the incoming handler is big enough for a thread or not. What is Microsoft's MVP variant of MVC? --=20 Bye, Gor Gyolchanyan. --047d7b603ebc40bed904c5b91f0c Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable <div class=3D"gmail_quote">On Thu, Jul 26, 2012 at 2:15 PM, Russel Winder <= span dir=3D"ltr">&lt;<a href=3D"mailto:russel winder.org.uk" target=3D"_bla= nk">russel winder.org.uk</a>&gt;</span> wrote:<br><blockquote class=3D"gmai= l_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left= :1ex"> <div class=3D"im">On Thu, 2012-07-26 at 13:45 +0400, Gor Gyolchanyan wrote:= <br> <br> &gt; So the good way to do this will be to have a single thread, which pump= s<br> &gt; messages and distributes the appropriate message handlers to worker<br=

&gt;<br> &gt; My goal here is to have 100% working and responsive GUI no matter how = bad<br> &gt; the application lags. If, for instance, The user would push a button, = which<br> &gt; initiates a very expensive computation, I don&#39;t want the GUI to be= come<br> &gt; stuck. If the user doesn&#39;t wait and pushes the button again, it sh= ould<br> &gt; display a message, which says &quot;The operation is already in progre= ss&quot; or<br> &gt; something like that.<br> <br> </div>Your goal is excellent. Throughout the early 2000s my staff had to<br=

to take this message on.<br> <br> The &quot;standard model&quot; for doing this is for user event callbacks t= o<br> always act and if they need to do something that takes longer than a<br> very few instructions and/or takes more that a fraction of second (NB<br> user reaction time is around 0.2s, user boredom time is around 2s) spawn<br=

thread puts an event on the event queue and terminates.<br> <br> Separation of concerns is also important here: no business logic in the<br> GUI code, no GUI code in the business logic. MVC (*), Mediator, Fa=C3=A7ade= ,<br> are your friends.<br> <br> (*) Microsoft&#39;s MVP variant of MVC is probably very appropriate on<br> Windows.<br> <div class=3D"HOEnZb"><div class=3D"h5">--<br> Russel.<br> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D<br> Dr Russel Winder =C2=A0 =C2=A0 =C2=A0t: +44 20 7585 2200 =C2=A0 voip: <a hr= ef=3D"mailto:sip%3Arussel.winder ekiga.net">sip:russel.winder ekiga.net</a>= <br> 41 Buckmaster Road =C2=A0 =C2=A0m: +44 7770 465 077 =C2=A0 xmpp: <a href=3D= "mailto:russel winder.org.uk">russel winder.org.uk</a><br> London SW11 1EN, UK =C2=A0 w: <a href=3D"http://www.russel.org.uk" target= =3D"_blank">www.russel.org.uk</a> =C2=A0skype: russel_winder<br> </div></div></blockquote></div><br>I totally agree with separation of GUI a= nd logic. The problem is, that the engine should be generic, so there&#39;s= no way of determining whether the incoming handler is big enough for a thr= ead or not.<div> <br></div><div>What is=C2=A0Microsoft&#39;s MVP variant of MVC?<br><div><di= v><br></div>-- <br>Bye,<br>Gor Gyolchanyan.<br> </div></div> --047d7b603ebc40bed904c5b91f0c--
Jul 26 2012
prev sibling next sibling parent Russel Winder <russel winder.org.uk> writes:
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Thu, 2012-07-26 at 14:33 +0400, Gor Gyolchanyan wrote:
[=E2=80=A6]
 I totally agree with separation of GUI and logic. The problem is, that th=

 engine should be generic, so there's no way of determining whether the
 incoming handler is big enough for a thread or not.

But the engine should not be making those decisions, they have to be made by the callback.
 What is Microsoft's MVP variant of MVC?

Google is your friend ;-) http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter http://www.codeproject.com/Articles/288928/Differences-between-MVC-and-MVP-= for-Beginners http://stackoverflow.com/questions/2056/what-are-mvp-and-mvc-and-what-is-th= e-difference Sadly there is also a lot of real rubbish on the Web purporting to be quality information, as is highlighted in one or two of the answers on StackOverflow :-( --=20 Russel. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder ekiga.n= et 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder
Jul 26 2012
prev sibling next sibling parent "Kagamin" <spam here.lot> writes:
On Thursday, 26 July 2012 at 09:46:26 UTC, Gor Gyolchanyan wrote:
 So the good way to do this will be to have a single thread, 
 which pumps
 messages and distributes the appropriate message handlers to 
 worker
 threads, right?

 My goal here is to have 100% working and responsive GUI no 
 matter how bad
 the application lags. If, for instance, The user would push a 
 button, which
 initiates a very expensive computation, I don't want the GUI to 
 become
 stuck. If the user doesn't wait and pushes the button again, it 
 should
 display a message, which says "The operation is already in 
 progress" or
 something like that.

Are you sure you can create a complex window fast enough when every operation is asynchronous? I usually find applications built with complex ui frameworks start slow. Isn't it because it takes long to create a window going through all the framework infrastructure? About separation of ui logic and business logic I suggest to look at MVVM pattern (or PresentationModel according to Fowler's terminology). Well, it doesn't allow ad-hoc code and implies extensive design work for every View, but by ensuring that the ViewModel contains only plain data, you're guaranteed that business logic is separated from ui logic.
Jul 26 2012
prev sibling parent "Kagamin" <spam here.lot> writes:
On Thursday, 26 July 2012 at 17:15:23 UTC, Simon wrote:
 On 'doze every thread can have it's own message pump and 
 therefore it's own UI, but if you have an ancestor/descent 
 relation between 2 windows they must belong to the same thread.

I meant, you can, say, set window text from non-ui thread directly and it will probably succeed, while normally the control should check and throw if current thread doesn't match ui thread.
Jul 27 2012