Today, I suddenly realized that nstimer is a running method that is implemented in multiple threads or in the main thread. Of course, it cannot be as simple as while in the main thread, so nothing can be done. Simply put, nstimer is running in synchronous mode. The time is up. After the message is sent, the ontimer function is called on the main thread.
We often see this Code:
- -(Ibaction) Start :( ID) sender
- {
- Pagestillloading = yes;
- [Nsthread detachnewthreadselector: @ selector (loadpageinbackground :) totarget: Self withobject: Nil];
- [Progress sethidden: No];
- While (pagestillloading ){
- [Nsunloop currentrunloop] runmode: nsdefaultrunloopmode beforedate: [nsdate distantfuture];
- }
- [Progress sethidden: Yes];
- }
Copy code
This piece of code is amazing because it will "Pause" the code and the program running will not be affected by a while loop. After [Progress sethidden: No] is executed, the entire function is paused and stops in the loop. After the operations in loadpageinbackground are completed, the [Progress
Sethidden: Yes] Run. This is a brief introduction and the logic is clear. If you do not do this, you need to call [Progress sethidden: Yes] In the loadpageinbackground to indicate that the load is complete. It seems that the Code is not compact and error-prone.
[Igoogle: the main thread of the application framework has encapsulated
Runmode: beforedate: Call; it and the while loop constitute a message pump, constantly get and process messages; it may be strange to everyone, since the main thread has encapsulated calls to the nsunloop, why can we call it again? This is the difference between it and Windows message loop. It can be called nested. when the while + nsunloop is called again, the program does not stop execution, and the program continuously extracts or processes messages. this is the same as the nested call of active scheduler in Symbian to achieve synchronization.]
Record a post: http://www.cocoachina.com/bbs/read.php? Tid-38499.html
Another example is as follows:
-(void)backgroundThreadStarted {
NSAutoreleasePool* thePool = [[NSAutoreleasePool alloc] init];
// create a scheduled timer
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(backgroundThreadFire:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
m_isBackgroundThreadToTerminate = NO;
// create the runloop
double resolution = 300.0;
BOOL isRunning;
do {
// run the loop!
NSDate* theNextDate = [NSDate dateWithTimeIntervalSinceNow:resolution];
isRunning = [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:theNextDate];
// occasionally re-create the autorelease pool whilst program is running
[thePool release];
thePool = [[NSAutoreleasePool alloc] init];
} while(isRunning==YES && m_isBackgroundThreadToTerminate==NO);
[thePool release];
}
-(void)backgroundThreadFire:(id)sender {
// do repeated work every one second here
// when thread is to terminate, call [self backgroundThreadTerminate];
}
-(void)backgroundThreadTerminate {
m_isBackgroundThreadToTerminate = YES;
CFRunLoopStop([[NSRunLoop currentRunLoop] getCFRunLoop]);
}