[racket] text-field% locking

From: Ivanyi Peter (pivanyi at freemail.hu)
Date: Sun Jun 5 14:08:34 EDT 2011

Dear All,

I have a problem.
I try to execute a command line program from my Scheme program.
The stdout and stderr of the command line program should 
be redirected to a text-field%. I have one program which
writes to stdout and stderr quite quickly and I get the
following error:

iproc.scm:79:24: sequence-contract-violation: negative: method insert cannot be called, except in states (unlocked), args "iProc ERROR:  Run Time:     0.00 seconds\r"

It seems, there is some problem with locking.
I also include the iproc.scm file, which executes the external
command line program.
What can I do to solve this problem???

Thanks for any help.

Best regards,

Peter Ivanyi

-------------------------------------------------

(module iproc scheme
  (require (lib  "process.ss")
           (file "sx-alert.scm")
  )
  
  (provide iproc)
  
  (define (iproc command editor)
    (let
      ( (proc     #f)
        (proc-in  #f)
        (proc-out #f)
        (proc-err #f)
        (thd      #f)
      )
      (begin
; For debugging
        (display "Command: ")
        (display command)
        (newline)
        
        ; start the process asynchronously
        (set! proc (process command))
        ; if we could start it
        (if (and proc (eq? 'running ((list-ref proc 4) 'status)))
          (begin
            (set! proc-in   (list-ref proc 0))
            (set! proc-out  (list-ref proc 1))
            (set! proc-err  (list-ref proc 3))
            ; a WATCHDOG thread to see whether the program is running
            (set! thd (thread
              (lambda ()
                (dynamic-wind
                  void;
                  (lambda ()
                    ; wait until the program is running
                    ((list-ref proc 4) 'wait)
                  )
                  (lambda ()
                    ;(display "SCM: finished watchdog\n")
                    (set! proc #f)
                    (close-output-port proc-out)
                  )
                )
              )
            )
            )
            ; OUTPUT from program
            (thread 
              (lambda ()
                (dynamic-wind
                  void
                  (lambda ()
                    (when editor
                      (do ((line (read-line proc-in) (read-line proc-in)))
                        ((eof-object? line))
                        (if editor
                          (send editor insert line)
                          (sx-alert #f 
                                  "iProc ERROR" 
                                  (string-append "iProc ERROR: " line)
                                  #f)
                        )
                      )
                    )
                  )
                  (lambda () (close-input-port proc-in)))))

            ; ERROR from program
            (thread 
              (lambda ()
                (dynamic-wind
                  void
                  (lambda () 
                    (do ((line (read-line proc-err) (read-line proc-err)))
                      ((eof-object? line))
                      (if editor
                        (send editor insert (string-append "iProc ERROR: " line))
                        (sx-alert #f 
                                  "iProc ERROR" 
                                  (string-append "iProc ERROR: " line)
                                  #f)
                      )
                    )
                  )
                  (lambda () (close-input-port proc-err)))))
          )
          (let
            ((error (string-append "iProc ERROR: Command '" command "' could not be executed!")))
            (if editor
              (send editor insert error)
              (sx-alert #f "iProc ERROR" (string-append "iProc ERROR: " error) #f)
            )
          )
        ) ; end of if running
      )
      
      (thread-wait thd)
      (sleep 0.5)
    )
  )  
)



Posted on the users mailing list.