Page 1 of 1

Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:13 pm
by Roby
Greetings to all the forum, this is my first post.
This is my problem:
With the following code I sometimes get the memory access error or external lib error if I try it in linux mint 21.3.
With Windows it works fine.
Does anyone know the reason?

I am using Purebasic 6.04

Code: Select all

Enumeration
  #Win_0
  #Btn_0
EndEnumeration

EnableExplicit
Define.i i,Event
Define.b srot

Procedure Anim_Win(srot)  ;srotola\arrotola la finestra
  Protected i
  If srot=1 ;srotola
    For i=136 To 520 Step 8
      ResizeWindow(#Win_0, #PB_Ignore, #PB_Ignore,#PB_Ignore,i)
      Delay(2)
    Next i  
  Else  ;arrotola
    For i=520 To 136 Step -8
      ResizeWindow(#Win_0, #PB_Ignore, #PB_Ignore,#PB_Ignore,i) 
      Delay(2)
    Next i 
  EndIf
 
EndProcedure
Procedure Win_0()
  If OpenWindow(#Win_0,0,0,500,136,"Prova thread",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    ButtonGadget(#Btn_0,230,100,20,15,"Srotola")
    
  EndIf
EndProcedure

Win_0()
  
Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Btn_0
          srot = srot ! 1
          CreateThread(@Anim_Win(), srot)
          ;Anim_Win(srot)
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow


Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:21 pm
by mk-soft
Welcome ;)

With Linux and macOS, windows and gadgets must not be changed in the thread. This leads to a crash.
OS intern restriction !

Use PostEvent or my module ThreadToGUI (see signature)

Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:32 pm
by Roby
OK, thank you 1000
I was breaking my head to figure out where the error was.

Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:41 pm
by infratec

Code: Select all

EnableExplicit

Enumeration
  #Win_0
  #Btn_0
EndEnumeration

Enumeration #PB_Event_FirstCustomValue
  #Event_AnimWin
EndEnumeration

Structure ThreadParameter_Strucrure
  Thread.i
  ;Semaphore.i
  ;Mutex.i
  srot.i
  ;Exit.i
EndStructure


Procedure Anim_Win(*Parameter.ThreadParameter_Strucrure)  ;srotola\arrotola la finestra
  
  Protected i
  
  
  If *Parameter\srot = 1 ;srotola
    For i=136 To 520 Step 8
      PostEvent(#Event_AnimWin, #Win_0, 0, 0, i)
      Delay(10)
    Next i  
  Else  ;arrotola
    For i=520 To 136 Step -8
      PostEvent(#Event_AnimWin, #Win_0, 0, 0, i)
      Delay(10)
    Next i 
  EndIf
 
EndProcedure

Procedure Win_0()
  If OpenWindow(#Win_0,0,0,500,136,"Prova thread",#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
    ButtonGadget(#Btn_0,230,100,50,25,"Srotola")
  EndIf
EndProcedure


Define.i i,Event
Define ThreadParameter.ThreadParameter_Strucrure

Win_0()
  
Repeat
  Event = WaitWindowEvent()
  
  Select Event
    Case #Event_AnimWin
      ResizeWindow(#Win_0, #PB_Ignore, #PB_Ignore,#PB_Ignore, EventData())
      
    Case #PB_Event_Gadget
      Select EventGadget()
        Case #Btn_0
          If Not IsThread(ThreadParameter\Thread)
            ThreadParameter\srot ! 1
            ThreadParameter\Thread = CreateThread(@Anim_Win(), @ThreadParameter)
          EndIf
      EndSelect
  EndSelect
  
Until Event = #PB_Event_CloseWindow

Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:50 pm
by Roby
Thank you infratec, I tried your code and it works fine.
I am now studying it to understand how it works.
You were really quick thank you again to everyone.

Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 4:54 pm
by mk-soft
Good ...
but small bug

Code: Select all

If Not IsThread(ThreadParameter\Thread)

Re: Error with thread if I resize windows with linux

Posted: Sun Mar 03, 2024 5:00 pm
by Roby
I still get the memory access error if I click the button quickly and repeatedly.
I think because multiple threads overlap if I quickly click the button.
But after mk-soft correction it would seem to work.

Re: Error with thread if I resize windows with linux

Posted: Mon Mar 04, 2024 10:28 am
by infratec
Corrected the bug in the listing.