Recent

Author Topic: [SOLVED] Multithreaded app on win10, disappearing GUI elements  (Read 3542 times)

TCH

  • Full Member
  • ***
  • Posts: 200
[SOLVED] Multithreaded app on win10, disappearing GUI elements
« on: September 04, 2017, 10:43:29 am »
Hey everyone!

I have a big problem. I have a "worktime terminal" which allows users to register events to their cards. The terminal uses TImages and some native GUI elements over them (Labels, Edits, etc.).

Originally it was operating with Timers, but after i realized, that the Timers are all runs inside the main thread, i rewrote it to multithreaded. (Mainly because the "ping" event froze the entire application, if there was a network, but the destination was unreachable...)
After this, some new gfx glitches appeared, which seems to be all random. The terminal works properly for rnd() time (minutes to weeks) and then, suddenly the GUI elements (Labels, Edits, etc.) begins to appear half-drawn or not drawn at all. I tried to call .Refresh, .Repaint, tried to resize them to their suppozed size, but all no avail.

There is an image about what i'm talking: http://oscomp.hu/depot/gfx_glitch.png

As you can see on this image, the label is drawn only partially. And i cannot debug the reason. It works under my virtual winxp (what i use for development), but on the win10 based terminals it does this.

I repeat, this has begun when i made the program multithreaded. Is anyone run into this kind of problem before? Does anyone have any idea? The customer is becoming very angry, so i appreciate any idea...
« Last Edit: September 15, 2017, 08:14:01 pm by TCH »

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #1 on: September 04, 2017, 11:38:05 am »
hi,
could it be that the main thread is doing something else besides handling the GUI?
are you firing events for communicating with main thread or how do you synchronize the GUI?
Lazarus 2.0.2 64b on Debian LXDE 10

Noodly

  • Jr. Member
  • **
  • Posts: 70
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #2 on: September 04, 2017, 12:03:39 pm »
Have you followed the guidelines here, especially the Synchronize procedure: http://wiki.freepascal.org/Multithreaded_Application_Tutorial?
Windows 10 Home, Lazarus 2.02 (svn 60954), FPC 3.04

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #3 on: September 04, 2017, 01:16:01 pm »
hi,
could it be that the main thread is doing something else besides handling the GUI?
are you firing events for communicating with main thread or how do you synchronize the GUI?
The main thread is responsible for dealing with the user input (clicks, keystrokes). It does some object handling (mostly text/caption and visibility settings) and some network communications.
The threads are also do some object handling (e.g. updating the clock), WAV playing and and network comm. The threads calls the same GUI controllong and netcomm procedures as the main thread.
Have you followed the guidelines here, especially the Synchronize procedure: http://wiki.freepascal.org/Multithreaded_Application_Tutorial?
No, i just googled how the TThread class works. Still i did mostly as this guide suggest, with the exception of the Synchronize, i don't use that. The threads just calls the same global procedures as the main thread.

Can that be the problem? For example the image you saw in my opening post is containing a label which was shown by the main thread and not a separate thread and it's still shows up badly. Also, the configuration panel - which is just a TImage and some Labels/Texts/Comboboxes/Checkboxes - is too, controlled by the main thread and the GUI elements sometimes disappears or drawn halfly.
If i don't use the Synchronize when dealing GUI elements inside threads, then can that screw up the main thread's GUI handling too?

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #4 on: September 04, 2017, 02:05:27 pm »
i think best practice is that the GUI should be left only to main thread.
use synchronization (ex. events) to signalize changes that should be considered for GUI to the main thread.
this will allow main thread to keep the GUI updated.
move network comms to new threads.

check also:
http://forum.lazarus.freepascal.org/index.php/topic,37615.msg253561.html#msg253561
Lazarus 2.0.2 64b on Debian LXDE 10

Mick

  • Jr. Member
  • **
  • Posts: 51
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #5 on: September 04, 2017, 02:07:23 pm »
Yes. Background thread cannot manipulate GUI for a couple of reasons. Use Synchronize. Or re-design your background threads to only handle network connections and data processing and signaling the main thread to do the GUI update.

TCH

  • Full Member
  • ***
  • Posts: 200
Re: Multithreaded app on win10, disappearing GUI elements
« Reply #6 on: September 04, 2017, 02:27:01 pm »
Okay guys, thanks for the tips, i'll try it instantly.

TCH

  • Full Member
  • ***
  • Posts: 200
Re: [SOLVED] Multithreaded app on win10, disappearing GUI elements
« Reply #7 on: September 15, 2017, 08:14:25 pm »
Okay, thanks for the tips, it solved it.

 

TinyPortal © 2005-2018