Creating gadgets in a thread procedure

Just starting out? Need help? Post your questions and find answers here.
Mowen
User
User
Posts: 48
Joined: Tue Oct 07, 2003 1:04 pm
Location: Belgium

Creating gadgets in a thread procedure

Post 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
PureBasic: one of the best programming tools ever ! PB is light, easy, crossplatform, powerfull, fast, extendable, enjoyable and... tasty ;-)
El_Choni
TailBite Expert
TailBite Expert
Posts: 1007
Joined: Fri Apr 25, 2003 6:09 pm
Location: Spain

Post 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...
El_Choni
Mowen
User
User
Posts: 48
Joined: Tue Oct 07, 2003 1:04 pm
Location: Belgium

Post by Mowen »

I tried with a mutex and and the threadsafe option but no improvements. :(
PureBasic: one of the best programming tools ever ! PB is light, easy, crossplatform, powerfull, fast, extendable, enjoyable and... tasty ;-)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
Fred
Administrator
Administrator
Posts: 18153
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Don't forget to turn on the 'threadsafe' switch as well.
Post Reply