home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sys / next / programm / 7718 < prev    next >
Encoding:
Internet Message Format  |  1992-12-13  |  2.1 KB

  1. Path: sparky!uunet!rosie!next.com
  2. From: jdoenias@next.com (Josh Doenias)
  3. Newsgroups: comp.sys.next.programmer
  4. Subject: Re: How do I make my "stop" button work?
  5. Message-ID: <6032@rosie.NeXT.COM>
  6. Date: 14 Dec 92 01:36:45 GMT
  7. References: <BywsHq.Hv4@cmcl2.nyu.edu>
  8. Sender: news@NeXT.COM
  9. Lines: 67
  10.  
  11. In article <BywsHq.Hv4@cmcl2.nyu.edu> dario@cns.nyu.edu (Dario Ringach,  
  12. x3941) writes:
  13. >I have a button sending an -abortCalculation message to an object which
  14. >is engaged in a loop...  Something like this:
  15. >
  16. >-compute:sender
  17. >{
  18. >  notAborted = YES;
  19. >  while(notAborted) {
  20. >    .... 
  21. >  }
  22. >  return self;
  23. >}
  24. >
  25. >-abortCalculation:sender
  26. >{
  27. >  notAborted = NO;
  28. >  return self;
  29. >}
  30. >
  31. >Needless to say... it doesn't work.  
  32. >How can I make it to work?
  33.  
  34.  
  35. Your abortCaclulation: message is the target of a button.  It will get  
  36. called as a result of a user action -- namely, the user clicking on the  
  37. button.
  38. However, your app is busy in its while(notAborted) loop, so it can't  
  39. respond to user actions.  You have to give the app the opportunity to  
  40. respond to events.
  41.  
  42. You can do this in one of several different ways, but probably the easiest  
  43. and most appropriate for this situation is to use a modal session.
  44.  
  45. A modal session allows you to run your loop while still processing events.   
  46. You can only respond to events in one window with this method.  That's  
  47. probably okay, since all you care about is the window with the stop button  
  48. in it.
  49. This would end up looking something like this:
  50.  
  51. - compute:sender
  52. {
  53.   NXModalSession session;
  54.   
  55.   [NXApp beginModalSession:&session for:theWindow];  // theWindow is the  
  56. window with the stop button in it. 
  57.   while ([NXApp runModalSession:&session] == NX_RUNCONTINUES) {
  58.     /* Do the rest of your computation loop here */
  59.     }
  60.   [NXApp endModalSession:&session];
  61.   return self;
  62. }
  63.  
  64. - abortCalculation:sender
  65. {
  66.   [NXApp stopModal];
  67.   return self;
  68. }
  69.  
  70.  
  71. For a slightly different way to make an abortable procedure, check out the  
  72. description of NXUserAborted().  This will let you break out of a loop if  
  73. the user hits cmd - period.  
  74.  
  75. Hope this helps,
  76.  
  77. -- josh
  78.