Professional Web Applications Themes

NSWindow in new thread - Mac Programming

Hello NG In my application I have a NSWindowController class to have multiple windows doing the same thing. (like safari if you press command N) Each window has it's own set of resources and objects that is needs to run. Now I want that they run in separate threads ( or task ?) . In the main nib I have a instance of the controlling class that makes up a new window if you press command N. The function to start the new window: -(IBAction) newBrowser:(id)sender{ servers * nServers = [nServerAction nServers]; articleAction * newWindow = [[articleAction alloc] initWithWindowNibName:"NewsBrowser" andGroups:theGroups ...

  1. #1

    Default NSWindow in new thread

    Hello NG

    In my application I have a NSWindowController class to have multiple windows doing
    the same thing.
    (like safari if you press command N)
    Each window has it's own set of resources and objects that is needs to run.
    Now I want that they run in separate threads ( or task ?) . In the main nib I have a
    instance of the controlling class that makes up a new window if you press command N.
    The function to start the new window:

    -(IBAction) newBrowser:(id)sender{
    servers * nServers = [nServerAction nServers];
    articleAction * newWindow = [[articleAction alloc]
    initWithWindowNibName:"NewsBrowser" andGroups:theGroups andServers:nServers];
    [newWindow showWindow:self];
    }
    is only the creation of a new instance of a NSWindowController subclass object with
    nib.

    How can i detach the new window and have it in a separate thread for live time?

    Thank you for your time and help

    Florian



    Florian Guest

  2. #2

    Default Re: NSWindow in new thread

    Florian Zschocke <de> wrote:
     

    Answer: You kind-of can't. You need to make a thread for whatever's
    being represented in the window, and then have the thread update the
    window (or vice-versa).

    Read up on AppKit thread safety and general Cocoa threading info:
    <http://www.cocoadev.com/index.pl?ThreadSafety>
    Paul Guest

  3. #3

    Default Re: NSWindow in new thread

    Paul Mitchum wrote: 
    >
    > Answer: You kind-of can't.[/ref]

    yeah, that's my impression too. my conservatie approach is to treat all
    cocoa objects, and in fact the entire user interface, as non-reentrant
    and usable only from the program's main thread. it's not *too* bad, if
    you can dream up decent communication methods to talk to background
    threads, which you have to consider "headless."

    i haven't gotten far enough yet to know for sure, but i hope that's not
    true for files. if you can't manipulate files in background threads, i
    am going to be royally ed. seems like it ought to work though, if
    you can stick to the bsd file functions. i'll also need to make use of
    berkeley sockets in background threads.

    useless side note: the beos equivalent to NSWindow is BWindow, and every
    single one of them runs in its own thread, whether you want it to or
    not. just as there is no way in cocoa to run an NSWindow in its own
    thread, there is no way in beos to NOT run a BWindow in its own thread.
    people porting code from other platforms would find that nearly
    unbelievable, and go to great lengths to defeat the potential race
    conditions. it's better not to fight it, and do things the beos way.
    but i am really one to talk, because i am *definitely* not doing things
    the cocoa way.
    Jhnny Guest

  4. #4

    Default Re: NSWindow in new thread

    Jhnny Fvrt (it means "halo, then resonate") <com> wrote: 
    Thank you Johnny,
    , I really thought that a window as separate task would be the most logic unit.
    So I have to rebuild the design, that will take a while........

    Florian Guest

  5. #5

    Default Re: NSWindow in new thread

    c0m (Paul Mitchum) wrote: 
    Florian Zschocke <de> wrote: 
    >
    >Answer: You kind-of can't. You need to make a thread for whatever's
    >being represented in the window, and then have the thread update the
    >window (or vice-versa).[/ref]

    Thank you Paul.
    I have to think about that, course in the moment I don't see where to start. I thought a
    window would be the most logic unit for a thread. 
    I will read that first.

    Florian

    Florian Guest

  6. #6

    Default Re: NSWindow in new thread

    c0m (Paul Mitchum) wrote: 
    Thank you Paul
    Not nice, in the moment I really don't know where to start...... 
    ......I will read that first.

    Florian

    Florian Guest

  7. #7

    Default Re: NSWindow in new thread

    On Thu, 15 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     

    Using either Distributed Objects for inter-thread communications, or the
    mundane but easier-to-set-up performSelectorOnMainThread:, you can
    *almost* pretend that all of your subthreads work like "normal". You just
    have to make sure that any time you talk to a non-thread-safe Cocoa object
    that you either talk to a proxy (if you're using DO) or you use
    performSelectorOnMainThread:.

    If you already know all of that, just ignore it and consider that I'm
    writing to all of the lurkers out there. :)
    Michael Guest

  8. #8

    Default Re: NSWindow in new thread

    Michael Ash wrote: 

    i can sort of see how that might work conceptually, but i'm having
    trouble imagining how i would put the information to use in the real
    world.

    this would be a typical background thread: the user wants to download
    messages from an nntp or smtp server. i fire up a separate thread to do
    the work, so that the ui doesn't get sluggish. that thread will need to
    pop up a status window showing how things are going, and it will have a
    button that allows the user to terminate the action. i figured i'd
    solve it by creating an ingoing and outgoing queue of messages,
    protected by semaphores, that the ui and the background thread can check
    at their leisure.

    on the ui side, i guess that would mean i'd have to use timer messages
    to give myself a place to poll the queue. actually, that doesn't sound
    so good. is there a way for a background thread to send NSEvents to the
    main app thread?

    that was my general conception of how to go about it up to now. how
    would i do it with either DO or performSelectorOnMainThread:?
    Jhnny Guest

  9. #9

    Default Re: NSWindow in new thread

    On Fri, 16 Jul 2004, =?ISO-8859-1?Q?J=F8hnny_F=E4v=F2r=EDt=EA_=28it_means_=22halo= 2C_then_resonate=22=29?= wrote:
     

    Your situation is a bit too complicated for performSelectorOnMainThread:,
    I think, which can only send messages in one direction, from your worker
    thread to the main thread.

    I assume you can set up the Distributed Objects connection yourself; Apple
    has sample code that shows how to set up communication between threads,
    and you can pretty much just copy/paste it. Once you get the DO connection
    set up and you have proxies to the appropriate objects in the other
    thread, communication is as easy as this:

    [mainThreadControllerProxy worker:self setProgress:0.5]

    Or perhaps something like this:

    [[mainThreadController progressIndicatorForWorker:self] setFloatValue:0.5]

    The DO system will make sure that all objects returned from cross-thread
    calls will be proxied, so once you get ahold of a pointer to a GUI object
    from a DO proxy, that pointer will itself actually be a proxy that will
    run everything in the main thread. And for your main thread to communicate
    to your worker, to cancel it or whatever, once the connection is set up:

    [workerProxy cancel]

    Or whatever else you'd like to send. DO is a bit tricky to set up, and
    there are some gotchas, but for the most part you can send messages like
    usual (to the proxies) and it just works.

    This is a pretty vague explanation, because DO is very general and you can
    do this sort of thing in a lot of different ways. If it sounds
    interesting, do have a look and ask if you have questions about it.
    Michael Guest

  10. #10

    Default Re: NSWindow in new thread

    On 2004-07-14 08:21:40 -0700, Florian Zschocke <de> said: 

    Some very minor style notes on the above code (that won't impact its
    functionality):

    * Generally, Objective-C class names should start with a capital letter
    to distinguish them from variables.

    * You've got the method naming pattern down, though typically Cocoa
    developers leave out conjunctions and articles in method names. In
    other words, many would use initWithWindowNibName:groups:servers:
    above. Prepositions (to, with, for), on the other hand, tend to be
    placed in front of argument names. There's a style guide on
    <http://developer.apple.com/> that includes method naming guidelines,
    though it's not a big deal. (Knowing the patterns can be helpful
    though because it can help you predict what the name of a method you
    might want to use will be.)

    * Feel free to use long, descriptive class & method names, like
    "newsServers" instead of "nServers". It can really enhance readability
    and make new code easier to write.

    Also, are you trying to follow a command pattern in the above code, by
    instantiating an "action" and then doing something with it? Many times
    that kind of pattern is less necessary in Cocoa between the
    target-action pattern and undo manager support provided by the
    framework. But it can still be useful at times, too.
     

    What you really want to run in a separate thread isn't your window, but
    rather the data model "behind" the window -- that is, the data model
    supplying the controls in the window with information to display. You
    can detach a thread very easily in Cocoa, using +[NSThread
    detachNewThreadSelector:toTarget:withArgument:]. Just make sure you're
    still doing your user interface work in the main thread, and read
    Apple's guidelines on multithreading in Cocoa on
    <http://developer.apple.com/> and you should be fine.

    -- Chris

    --
    Chris Hanson <com>
    http://www.livejournal.com/users/chanson/


    Chris Guest

  11. #11

    Default Re: NSWindow in new thread

    On 2004-07-16 07:19:17 -0700, Jhnny Fvrt (it means "halo, then
    resonate") <com> said:
     

    One way this would be done in Cocoa is to create a separate thread to
    do the work on or with your model objects. Periodically the thread
    uses -[NSObject performSelectorOnMainThread:withObject:waitUntilDo ne:]
    to inform a controller that the state of the model has changed. The
    controller - in the main thread - can update the interface then to
    match the new state of the model objects. Of course you have to be
    careful about synchronizing access to shared data (the model objects),
    but that's fairly straightforward for a case like this.

    The point is that the controller is actually (a) the thing that asks
    the model to do the work (which results in a new thread at the model
    layer) and (b) the thing that puts up the status window. It could just
    as easily report status by playing a song, or by animating the cursor,
    or printing dots to stdout; the model doesn't care.
     

    That's pretty standard. As an added bonus, you're not conflating model
    and view code like in your above example.

    -- Chris

    --
    Chris Hanson <com>
    http://www.livejournal.com/users/chanson/


    Chris Guest

  12. #12

    Default Re: NSWindow in new thread

    Chris Hanson <com> wrote: 
    Thank you Chris 

    Yes, you are right. Some of the code is from the very beginning and I will have a cleanup
    before any other will read it. Even my english is very bad but i will try to explain things more
    concrete in future.
     
    I'm not sure what you mean but I really need these instances in the new Window and I need
    it from the beginning. Sure i also could write a method like setNewsGroups but that will
    make no difference.
     
    >
    >What you really want to run in a separate thread isn't your window, but
    >rather the data model "behind" the window -- that is, the data model
    >supplying the controls in the window with information to display. You
    >can detach a thread very easily in Cocoa, using +[NSThread
    >detachNewThreadSelector:toTarget:withArgument:]. Just make sure you're
    >still doing your user interface work in the main thread, and read
    >Apple's guidelines on multithreading in Cocoa on
    ><http://developer.apple.com/> and you should be fine.
    >[/ref]
    Yes, that was my biggest misunderstanding ever. I thought and even still think that a
    window is natural logical unit and it would be nice if it was more independent than it is in
    cocoa.

    Florian

    Florian Guest

  13. #13

    Default Re: NSWindow in new thread

    On 2004-07-17 02:23:06 -0700, Florian Zschocke <de> said: 

    Your English is fine, you're perfectly understandable.
     

    Cocoa really wants your code to follow the Model-View-Controller
    pattern. In this sense, a window is just something that's used to
    display information from the model code. So if you need a thread to do
    some work somewhere, it's generally in or related to the model code.
    At least that's the pattern in Cocoa. It's essentially the same reason
    you don't subclass NSWindow just because you're putting some controls
    in it.

    -- Chris

    --
    Chris Hanson <com>
    http://www.livejournal.com/users/chanson/


    Chris Guest

  14. #14

    Default Re: NSWindow in new thread

    On 17/07/2004, Florian Zschocke wrote in message
    <40f8effa$0$19728$arcor-online.net>:
     

    If it helps, the /actual/ code which draws your window is indeed
    handled by a dedicated thread. Window-drawing is not really handled
    by your program at all, it's handled by a GUI process which does all
    the display work and knows about how to handle minimised windows,
    how to draw shadows, and all that stuff. Drawing instructions do
    their drawing by sending instructions to this other process.

    Simon.
    --
    Using pre-release version of newsreader.
    Please tell me if it does weird things.
    Simon Guest

Similar Threads

  1. Newbie:Using ASP.NET thread pool thread to dispatch TCP data, etc.
    By Navin Mishra in forum ASP.NET Web Services
    Replies: 0
    Last Post: March 22nd, 06:45 PM

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