Threads running on OS X 10.9

Mac OSX specific forum
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Threads running on OS X 10.9

Post by J. Baker »

Anyone else having issues with threads crashing on OS X 10.9? I can run the same code all day long on OS X 10.6.8. I made sure "App Nap" wasn't the issue by selecting "Prevent App Nap" but it still crashes. I'll have to write a smaller code example and see if I get the same issue. If so, I'll post it.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Threads running on OS X 10.9

Post by J. Baker »

Ok, here's the code to test. It runs just fine on OS X 10.6.8. On OS X 10.9.3 (Developer Release) it has the following issues...
  1. EditorGadget() doesn't retain focus until it has been running for about 20 seconds or so. You have to keep clicking your mouse on the EditorGadget() to refresh it.
  2. The EditorGadget() never shows a scroll bar.
  3. The thread pretty much quits working after 49 seconds.
Can anyone with OS X 10.9.2 test this out and post results? This thread doesn't crash like my other code does but it does show issues. Maybe it has more to do with the EditorGadget() then the actual thread itself?

Code: Select all

Procedure Run(Parameter)
  
  StartTime = ElapsedMilliseconds()
  
 Repeat
  
  If ElapsedMilliseconds() - StartTime >= 1000
     StartTime = ElapsedMilliseconds()
     AddGadgetItem(0, CountGadgetItems(0), Str(ElapsedMilliseconds() / 1000))
     PostEvent(#PB_Event_Gadget, 0, 0, #PB_EventType_Change)
  EndIf
  
  Delay(1)
  
 ForEver
  
EndProcedure

If OpenWindow(0, 0, 0, 420, 270, "Thread Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 400, 250)
    
  CreateThread(@Run(), Parameter)
  
 Repeat
   
   Event = WaitWindowEvent()
         
    Select Event
   
   Case #PB_Event_Gadget   
     Select EventGadget()
         
       Case 0
         If EventType() = #PB_EventType_Change
           Range.NSRange\location = Len(GetGadgetText(0))
           CocoaMessage(0, GadgetID(0), "scrollRangeToVisible:@", @Range)
         EndIf
         
     EndSelect
    EndSelect
     
    If Event = #PB_Event_CloseWindow
      Quit = 1
    EndIf
     
 Until Quit = 1
 
EndIf
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Threads running on OS X 10.9

Post by wilbert »

It looks like the problem is caused by adding the gadget items in another thread.
Try to add them in the main thread.
In your example code AddWindowTimer(#Window, Timer, Timeout) can also be used.

Code: Select all

Procedure Run(Parameter)
  
  StartTime = ElapsedMilliseconds()
  
 Repeat
  
  If ElapsedMilliseconds() - StartTime >= 1000
     StartTime = ElapsedMilliseconds()
     PostEvent(#PB_Event_Gadget, 0, 0, #PB_EventType_Change)
  EndIf
  
  Delay(1)
  
 ForEver
  
EndProcedure

If OpenWindow(0, 0, 0, 420, 270, "Thread Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 400, 250)
    
  CreateThread(@Run(), Parameter)
  
 Repeat
   
   Event = WaitWindowEvent()
         
    Select Event
   
   Case #PB_Event_Gadget   
     Select EventGadget()
         
       Case 0
         If EventType() = #PB_EventType_Change
           AddGadgetItem(0, -1, Str(ElapsedMilliseconds() / 1000)) 
           Range.NSRange\location = Len(GetGadgetText(0))
           CocoaMessage(0, GadgetID(0), "scrollRangeToVisible:@", @Range)
         EndIf
         
     EndSelect
    EndSelect
     
    If Event = #PB_Event_CloseWindow
      Quit = 1
    EndIf
     
 Until Quit = 1
 
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Threads running on OS X 10.9

Post by J. Baker »

Thanks Wilbert but my app has a little more "string information" then just ElapsedMilliseconds(). That's why I have the AddGadgetItem() within the thread. This was just a smaller example code posted here. Is there no way to have AddGadgetItem() within the thread on OS X 10.9?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Threads running on OS X 10.9

Post by wilbert »

It might help to add the lines using CocoaMessage

Code: Select all

Global.i TextView, TextStorage

Procedure Run(Parameter)
  
  StartTime = ElapsedMilliseconds()
  
  Repeat
    
    If ElapsedMilliseconds() - StartTime >= 1000
      StartTime = ElapsedMilliseconds()
      
      ItemString.s = Str(ElapsedMilliseconds() / 1000) + #LF$
      AttrString = CocoaMessage(0, CocoaMessage(0, 0, "NSAttributedString alloc"), "initWithString:$", @ItemString)
      CocoaMessage(0, TextStorage, "appendAttributedString:", AttrString)
      CocoaMessage(0, AttrString, "release")

      PostEvent(#PB_Event_Gadget, 0, 0, #PB_EventType_Change)
    EndIf
    
    Delay(1)
    
  ForEver
  
EndProcedure



If OpenWindow(0, 0, 0, 420, 270, "Thread Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 400, 250)
  TextView = GadgetID(0)
  TextStorage = CocoaMessage(0, TextView, "textStorage") 
  
  CreateThread(@Run(), Parameter)
  
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget   
        Select EventGadget()
            
          Case 0
            If EventType() = #PB_EventType_Change
              CocoaMessage(@Range.NSRange\location, TextStorage, "length")
              CocoaMessage(0, TextView, "scrollRangeToVisible:@", @Range)
            EndIf
            
        EndSelect
    EndSelect
    
    If Event = #PB_Event_CloseWindow
      Quit = 1
    EndIf
    
  Until Quit = 1
  
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Threads running on OS X 10.9

Post by J. Baker »

Thanks Wilbert, that works great! I'll have to try it in my app now. :D
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Threads running on OS X 10.9

Post by wilbert »

The advantage of this attributed string approach is that you can also format the text you add.

Code: Select all

#NSItalicFontMask = 1
#NSBoldFontMask = 2

Global.i TextView, TextStorage

Procedure Run(Parameter)
  
  StartTime = ElapsedMilliseconds()
  
  Repeat
    
    If ElapsedMilliseconds() - StartTime >= 1000
      StartTime = ElapsedMilliseconds()
      
      ItemString.s = Str(ElapsedMilliseconds() / 1000) + #LF$
      
      AttrString = CocoaMessage(0, CocoaMessage(0, 0, "NSMutableAttributedString alloc"), "initWithString:$", @ItemString)
      CocoaMessage(@AttrRange.NSRange\length, AttrString, "length")
      CocoaMessage(0, AttrString, "applyFontTraits:", #NSBoldFontMask | #NSItalicFontMask, "range:@", @AttrRange)
      
      CocoaMessage(0, TextStorage, "appendAttributedString:", AttrString)
      CocoaMessage(0, AttrString, "release")

      PostEvent(#PB_Event_Gadget, 0, 0, #PB_EventType_Change)
    EndIf
    
    Delay(1)
    
  ForEver
  
EndProcedure



If OpenWindow(0, 0, 0, 420, 270, "Thread Test", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
  
  EditorGadget(0, 10, 10, 400, 250)
  TextView = GadgetID(0)
  TextStorage = CocoaMessage(0, TextView, "textStorage") 
  
  CreateThread(@Run(), Parameter)
  
  Repeat
    
    Event = WaitWindowEvent()
    
    Select Event
        
      Case #PB_Event_Gadget   
        Select EventGadget()
            
          Case 0
            If EventType() = #PB_EventType_Change
              CocoaMessage(@Range.NSRange\location, TextStorage, "length")
              CocoaMessage(0, TextView, "scrollRangeToVisible:@", @Range)
            EndIf
            
        EndSelect
    EndSelect
    
    If Event = #PB_Event_CloseWindow
      Quit = 1
    EndIf
    
  Until Quit = 1
  
EndIf
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Threads running on OS X 10.9

Post by J. Baker »

Very nice, thanks again! ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2196
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Re: Threads running on OS X 10.9

Post by J. Baker »

The CocoaMessage() fixed the AddGadgetItem() error but threads still crash on OS X 10.9.3 for some reason. Still looking into this.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
Post Reply