; Function : SetPriority { priority }

; Author : Peter Thor - email?
;          priority check added by JLB

; Sets the priority of your program to whatever you want.
; Negative numbers mean higher priority (roughly : more CPU
; time). Priority can be from -127 to +127. Positive numbers
; mean higher priority. It's recommended that most programs
; shouldn't use higher than 5-10 as far as I remember, but
; as long as you know what you're doing, you can crank it
; up to whatever you want (up to 127!), or make it low if
; it doesn't need much CPU time).

; Returns value of priority before function was called, so
; you just call SetPriority {returned value} to put it
; back how it was.

; Couldn't really decide how to return a failure, so just
; returns 0, which is still a valid value! Adjust it to suit
; your needs!

Function.w SetPriority{newpriority.w}

  If newpriority<-127 OR newpriority>127 Then Function Return 0

  Forbid_                     ;lock system to check for task

  *task.l=FindTask_(*crap.l)  ;*crap.l is only a NULL-Pointer
                              ;this way the task of the program itself
                              ;is returned

; set the new priority:
  oldpriority.w=SetTaskPri_(*task,newpriority.w)

  Permit_                     ;and return the system

Function Return oldpriority.w

End Function

; demo :

;; NOTE : Use XOpa or similar program to see priority. If you're
;; running from Blitz, it'll be the "Blitz ][ Program Proc" you're
;; looking for. With most of these type of programs, you'll have
;; to update the task list to see the change.

;; I use Executive, which I think modifies the priority you
;; set, so I sometimes get some weird number listed, but it
;; basically works!

; Repeat

;   Print "Priority (-127 to 127), 1000 to end : "
;   a.w=Edit(4)
;   If a=1000 Then End

;   oldpri.w=SetPriority {a}
;   NPrint "The old priority was : ",oldpri
;   NPrint ""

; Forever


