Mac how to design START & STOP function?

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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?
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
What kind of operation, what language, what framework? More context please. Tell what you are trying to do.
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
What kind of operation, what language, what framework? More context please. Tell what you are trying to do.
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?
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
downloading is concurrent with other part of application, i think we need thread here right?
Yes it sounds like you need it, in some form or your main thread will be locked up while this is taking place.

runing and stoping thread is a good solution? or to use NSTimer in START function and disable it throgh STOP function?
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.
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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?
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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?
 

neowin

macrumors newbie
Jun 27, 2011
19
0
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
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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?
read the third post my friend, its regarding webpage downloading.
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
read the third post my friend, its regarding webpage downloading.
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?
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
This sounds like a follow on from NSURLDownload problem!.
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.

----------

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?
yes all you said is right, but as i said it download webpages not file!
 

jared_kipe

macrumors 68030
Dec 8, 2003
2,967
1
Seattle
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?
 

saleh.hi.62

macrumors member
Original poster
Jul 25, 2011
95
0
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?
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.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
what is thew best algorithm for that?
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.