Page 1 of 1

Threads running on OS X 10.9

Posted: Tue Apr 01, 2014 2:58 am
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.

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 5:23 am
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

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 6:45 am
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

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 4:01 pm
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?

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 4:36 pm
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

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 4:44 pm
by J. Baker
Thanks Wilbert, that works great! I'll have to try it in my app now. :D

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 5:18 pm
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

Re: Threads running on OS X 10.9

Posted: Wed Apr 02, 2014 5:39 pm
by J. Baker
Very nice, thanks again! ;)

Re: Threads running on OS X 10.9

Posted: Sat Apr 05, 2014 2:16 am
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.