Error with thread if I resize windows with linux

Just starting out? Need help? Post your questions and find answers here.
Roby
New User
New User
Posts: 5
Joined: Fri Feb 09, 2024 9:16 am
Location: Italy

Error with thread if I resize windows with linux

Post 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

User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error with thread if I resize windows with linux

Post 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)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Roby
New User
New User
Posts: 5
Joined: Fri Feb 09, 2024 9:16 am
Location: Italy

Re: Error with thread if I resize windows with linux

Post by Roby »

OK, thank you 1000
I was breaking my head to figure out where the error was.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Error with thread if I resize windows with linux

Post 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
Last edited by infratec on Mon Mar 04, 2024 10:30 am, edited 2 times in total.
Roby
New User
New User
Posts: 5
Joined: Fri Feb 09, 2024 9:16 am
Location: Italy

Re: Error with thread if I resize windows with linux

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6205
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Error with thread if I resize windows with linux

Post by mk-soft »

Good ...
but small bug

Code: Select all

If Not IsThread(ThreadParameter\Thread)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Roby
New User
New User
Posts: 5
Joined: Fri Feb 09, 2024 9:16 am
Location: Italy

Re: Error with thread if I resize windows with linux

Post 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.
infratec
Always Here
Always Here
Posts: 7582
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Error with thread if I resize windows with linux

Post by infratec »

Corrected the bug in the listing.
Post Reply