Professional Web Applications Themes

NSTask output -> NSTextView - Mac Programming

Hi, I am trying to wrap a unix command in a Cocoa GUI and display the output in an NSTextView, but am running in some problems. Most of it works fine, but although I am using a separate thread to p the output and performSelectorOnMainThread to display it, I cannot interrupt the task with the Cancel button. So I created a small test project that runs the command "ls -la -R /Library" and displays the output. I added the possiblilty to use a thread to display the data by using the flag USE_THREAD. I would appreciate it if someone could ...

  1. #1

    Default NSTask output -> NSTextView

    Hi,

    I am trying to wrap a unix command in a Cocoa GUI and display the output
    in an NSTextView, but am running in some problems. Most of it works
    fine, but although I am using a separate thread to p the output and
    performSelectorOnMainThread to display it, I cannot interrupt the task
    with the Cancel button.

    So I created a small test project that runs the command "ls -la -R
    /Library" and displays the output. I added the possiblilty to use a
    thread to display the data by using the flag USE_THREAD.

    I would appreciate it if someone could have a look at the code and see
    if they can reproduce this, and even better, spot the error I made. You
    can find it at

    http://home.earthlink.net/~kvddrift/software/threadTest.tar.gz



    many thanks,

    - Koen.
    Koen Guest

  2. #2

    Default Re: NSTask output -> NSTextView

    In article <east.earthlink.net>,
    earthlink.net says... 

    I've not looked at your code but here's my idea. You've spawned an 'ls'
    process so cache its pid. When you hit cancel send that pid an
    appropriate signal. Read 'man 2 kill' for more details.
    James Guest

  3. #3

    Default Re: NSTask output -> NSTextView

    On 2004-06-13 16:31:24 +0200, Koen <earthlink.net> said:
     
     

    Koen,

    You go to much in the main loop. The main loop is completely saturated
    by your calls and can't answer anymore to the cancel button. Remove all
    your code braketed in USE_THREAD, it is not necessary. Change your
    updateResultsTextView as follow, and add a the method scrollToVisible
    and a insatnce variable 'timer'... Now it works as it should do !!!

    Regards, Luc.

    -(void)updateResultsTextView:(NSString *)aString
    {
    NSRange theEnd = NSMakeRange([[resultsTextView string] length], 0);

    [resultsTextView replaceCharactersInRange: theEnd withString: aString];

    theEnd.location += [aString length];

    // refresh view sometimes (not everytime !!!)
    if( nil == timer)
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
    selector:selector(scrollToVisible)
    userInfo:nil repeats:NO];
    }

    - (void)scrollToVisible {
    timer = nil;
    [resultsTextView scrollRangeToVisible:NSMakeRange([[resultsTextView
    string] length], 0)];
    }




    luc Guest

  4. #4

    Default Re: NSTask output -> NSTextView

    On 2004-06-13 16:31:24 +0200, Koen <earthlink.net> said:
     


    You go too much in the main loop... Don't use your USE_THREAD code. But
    change the followings methods with a new instance variable timer.
    Now it works !!!


    -(void)updateResultsTextView:(NSString *)aString
    {
    NSRange theEnd = NSMakeRange([[resultsTextView string] length], 0);

    [resultsTextView replaceCharactersInRange: theEnd withString: aString];

    theEnd.location += [aString length];

    // refresh view sometimes (not everytime)
    if( nil == timer)
    timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
    selector:selector(scrollToVisible)
    userInfo:nil repeats:NO];
    }

    - (void)scrollToVisible {
    timer = nil;
    [resultsTextView scrollRangeToVisible:NSMakeRange([[resultsTextView
    string] length], 0)];
    }

    luc Guest

Similar Threads

  1. [newbie] NSTextView and Scroller
    By Wezzy in forum Mac Programming
    Replies: 3
    Last Post: October 15th, 10:47 PM
  2. delegate or subclass NSTextView?
    By None in forum Mac Programming
    Replies: 3
    Last Post: October 1st, 08:58 AM
  3. NSTextView problems plus a few others...
    By Per Bull Holmen in forum Mac Programming
    Replies: 6
    Last Post: September 18th, 12:01 AM
  4. NSMutableAttributedString in an NSTextView
    By matt neuburg in forum Mac Programming
    Replies: 11
    Last Post: July 26th, 01:21 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139