Page 1 of 1

Creating gadgets in a thread procedure

Posted: Wed Mar 29, 2006 9:59 am
by Mowen
Hi,

I have strange behaviour when I create image gadgets in a threaded procedure. Images are loaded and progressively shown on screen but after the end of the procedure they disappear ! Outside a thread it works but I need to use threads for this...

Try this code (compile with PB4.0 beta8):

Code: Select all

EnableExplicit

Structure strImg
  fimg.l
  gimg.l
EndStructure

Global NewList Imgs.StrImg()
Global win, scr, thr

Procedure MyLoadImage(param)
  Protected i
  
  OpenGadgetList(param)
  For i = 1 To 10
    AddElement(Imgs())
    Imgs()\fimg = LoadImage(#PB_Any,"D:\My Documents\My Pictures\screen_108.jpg") ;Change the path to a valid jpeg image of your own
    ResizeImage(Imgs()\fimg, 60, 40)
    Imgs()\gimg = ImageGadget(#PB_Any,0,(i-1)*40,60,40,ImageID(Imgs()\fimg))
  Next
  CloseGadgetList()
EndProcedure

UseJPEGImageDecoder()
win = OpenWindow(#PB_Any,100,100,150,450,"Test")
CreateGadgetList(WindowID(win))
scr = ScrollAreaGadget(#PB_Any,10,10,100,400,60,400,40)
thr = CreateThread(@MyLoadImage(), scr)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

End

Posted: Wed Mar 29, 2006 10:16 am
by El_Choni
In the procedure PB is probably calling some GDI functions, which are not threadsafe. You could use mutexes or criticalsections for this. Although I'm not sure if this would fix the problem...

Posted: Wed Mar 29, 2006 10:20 am
by Mowen
I tried with a mutex and and the threadsafe option but no improvements. :(

Posted: Wed Mar 29, 2006 11:12 am
by freak
Using images in a thread is no problem, but you cannot create Gadgets inside a thread, that is a messaging problem.
(it works only if the whole window is created in a thread, and it has its own message loop)

It should work if you create the Gadget before the thread starts (with ImageID set to 0),
and then set the loaded image from inside the thread.

Posted: Wed Mar 29, 2006 12:50 pm
by Fred
Don't forget to turn on the 'threadsafe' switch as well.