how to design START & STOP function?

Discussion in 'Mac Programming' started by saleh.hi.62, Nov 16, 2011.

  1. saleh.hi.62 macrumors member

    Joined:
    Jul 25, 2011
    #1
    hello guys,

    i am trying to to write 2 function for starting an operation and when i call stop then it stop the operation.

    what is thew best algorithm for that?
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    What kind of operation, what language, what framework? More context please. Tell what you are trying to do.
     
  3. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #3
    okay, here it is.
    Code:
    -(void)start{
    // here it start downloading files from web occasionally. 
    }
    
    -(void)stop{
    // here it tries to stop this operation.
    }
    downloading is concurrent with other part of application, i think we need thread here right?

    runing and stoping thread is a good solution? or to use NSTimer in START function and disable it throgh STOP function?
     
  4. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #4
    Yes it sounds like you need it, in some form or your main thread will be locked up while this is taking place.

    If this is GUI application you will be provided with an event loop that checks the interface, such as buttons periodically. This is provided in the background however, so you don't really have to worry about it, you could add start and stop methods and connect them to corresponding start and stop buttons on the interface.
     
  5. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #5
    actually this is gonna be a library based on Fondation, so i want a general solution ;)

    how about nstimer? and disable that in my stop method?
     
  6. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #6
    Whats the best algorithm?? What are you doing? Is it something that blocks while waiting for something? Is it something that consumes a lot of cpu resources and you want to throttle it when you use your computer?
     
  7. neowin macrumors newbie

    Joined:
    Jun 27, 2011
    #7
    In main thread create global variable and set it to 1. Once you finish your function you can clear the variable. This variable is being monitored by another thread. You can use pthread to create this.
    This thread will be looping over the variable which is set to 1. Once the variable is made 0, you can exit the function.

    I know its complicated :p
     
  8. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #8
    read the third post my friend, its regarding webpage downloading.
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    Its downloading what? Are you trying to download a lot of results of HTTP requests? Are you trying to download a big file? Is stop going to kill the download and start resume, or restart?
     
  10. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
  11. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #11
    yes , i decided to download the webpages manually by NSXMLocument and save into a file not using NSUrlconnection or NSUrldownload.

    since i go for that, need to implement stop function.

    ----------

    yes all you said is right, but as i said it download webpages not file!
     
  12. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #12
    Resuming should not be a priority if the requested file/object isn't very large. NSURLDownload already has a method -cancel so just keep a list of unfinished url's and cancel all the running NSURLDownloads, -start should just make new NSURLDownloads for the ones that were cancelled.

    EDIT: You mentioned way back in the beginning that a thread may be required, well NSURLDowanload is asynchronous (aka threaded) and uses a NSURLDownloadDelegate to inform the creating object when it is done.

    What are you trying to do?
     
  13. saleh.hi.62 thread starter macrumors member

    Joined:
    Jul 25, 2011
    #13
    actually i have a hard time with NSURLDownload ! i still couldn't get it works! i decided to transfer the html file into NSString then save it into computer.

    thread was just a suggestion, its not my plan.
     
  14. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #14
    I can tell you want the best algorithm is not, or if you like, what the worst algorithm would be. That would be to start a synchronous download on a background thread in start, and then kill that thread in stop. Can you imagine what kind of mess that would leave behind? It might be okay if you did it because you were about to quit your application. But can you imagine how the mess would accumulate if you repeatedly did that over the life of an application?

    Saleh.hi.62 your problem is that if you don't understand something, your answer is to just go off and find something else. Your answer really should be do the hard mental work; take the time to read, research, and understand; and do it right.
     

Share This Page