The context :
On my mac app, when i click on a list item, a notification is sent to an object which does something in the background while on the UI there's a waiting message.
All of this takes place in a window which you can quit by a "Close" button. The button by default is disabled when the notifcation is sent.
What i a want to do is a timeout feature which allows the user to quit this windows after a couple minutes hence enabling the close button.
The code :
- (IBAction)onChangeOperator:(id)sender
{
[self performSelector:#selector(timerFired:) withObject:nil afterDelay:2.0];
....
....
//takes time
....
}
-(void) timerFired:(NSTimer *) theTimer {
[close_button setEnabled:YES];
}
The problem :
The button is not enabled until onChangeOperator is finished whereas i want it to be enabled as soon as selector is fired.
I think it's a thread thingy but i can't figure out.
From the documentation, performSelector:withObject:afterDelay:
Invokes a method of the receiver on the current thread using the default mode after a delay.
So the current thread is still blocked. You should instead run your expensive operation in onChangeOperator on a new thread:
- (IBAction)onChangeOperator:(id)sender
{
[self performSelector:#selector(timerFired:) withObject:nil afterDelay:2.0];
[self performSelectorInBackground:#selector(doUpdates) withObject:nil];
}
-(void) timerFired:(NSTimer *) theTimer
{
[close_button setEnabled:YES];
}
-(void)doUpdates
{
.... stuff that takes time....
}
Related
I have created a simple program about CDialog and Timer in MFC.
The problem I have encountered, I think it is very normal but i cannot explain how MFC handle message in one or many threads.
The mainly source code of program:
BOOL CMyDlg::OnInitDialog()
{
CDialog::OnInitDialog();
...
SetTimer(1, 10000, NULL);
return TRUE;
}
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BTN_START, OnBtnStartClicked)
END_MESSAGE_MAP()
void CMyDlg::OnBtnStartClicked()
{
DisplayMessage(1);
}
void CMyDlg::OnTimer(UINT nIDTimer)
{
if (nIDTimer == 1)
{
KillTimer(1);
DisplayMessage(2);
SetTimer(1, 10000, NULL);
}
}
void CMyDlg::DisplayMessage(INT nID)
{
if (nID == 1)
{
AfxMessageBox(_T("Button Clicked"));
}
else if (nID == 2)
{
AfxMessageBox(_T("Timer timeout"));
}
else
{
}
}
I debug program with following steps:
Set break points in two functions: OnBtnStartClicked() and OnTimer() and run in debug mode.
Click on Start button, the pointer of Visual Studio stops in OnBtnStartClicked(). Open Threads window, I see that the code is executed in "Main Thread".
Press F5 to continue. A message box is displayed. And I do nothing next.
In the next few seconds, the pointer of VS stops in OnTimer(). I also see in the Thread window and see that the code is also executed in "Main Thread".
Press F5 to continue. The second message box is display.
That makes me confused is: in Step 3, because I do nothing next, the "Main Thread" is temporary paused; but in Step 4, the "Main Thread" is continuously executed.
Please help me explain that make me confused!
Windows messaging is an event based system. Your program sits in a loop, checking the message queue and responding to events.
This means that when your program looks like it's waiting for you to click the OK button on the message box, it's still listening for and responding to events. One of those events would be the BN_CLICKED for the OK button, but another is a timer message.
If the program ever really pauses, your program will become unresponsive.
Maybe I mix too many different technologies together and run in some roadblock; some advise would be much appreciated.
I have an app which connects to several server; each connection with one input and output socket stream. The connection goes to a defined port and is close to telnet protocol. Text input/output. quite simple.
First I have an openStream function as wrapper called from main thread which create a client-specific GDC queue and dispatch the input/output-stream creation within that queue asynchronously:
gcdQueue = dispatch_queue_create([self.client.hostName cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_CONCURRENT);
// possible priorities:
// DISPATCH_QUEUE_PRIORITY_HIGH
// DISPATCH_QUEUE_PRIORITY_DEFAULT
// DISPATCH_QUEUE_PRIORITY_LOW
if (self.runASync)
{
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
dispatch_async(gcdQueue, ^{
[self openStreamsInternal];
});
}
Code for the technical open of steams
...
//
// in openStreamsInternal()
//
CFStreamCreatePairWithSocketToHost(NULL, (__bridge CFStringRef)self.client.hostName, [self.client.hostPort intValue], &_readStream, &_writeStream);
self.inputStream = (__bridge_transfer NSInputStream *)_readStream;
self.outputStream = (__bridge_transfer NSOutputStream *)_writeStream;
[self.inputStream setDelegate:self];
[self.outputStream setDelegate:self];
self.runLoop = [NSRunLoop currentRunLoop];
[self.inputStream scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode];
[self.outputStream scheduleInRunLoop:self.runLoop forMode:NSDefaultRunLoopMode];
[self.inputStream open];
[self.outputStream open];
// ... some lines later
if (self.runASync && (self.inputStream || self.outputStream))
{
[self.runLoop run];
}
I open both socket streams and link them to a runloop within the GCD-queue (assuming it will indirectly create a thread; not sure if that is always guaranteed).
Then via the delegate (a member function of my connection class) for the streams in
Code:
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode
I do what I have to do with in data flooding in. No issues until I stay in foreground. I close the streams when I go into background to release the resources.
Now with iOS 7 I want to enable background refresh for the streams. For that i don't close the streams anymore when moving into background and have the notification code in
Code:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(#"called in background for data fetch");
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
for (Connection *connection in self.document.clientList)
{
// Add a task to the group
[connection parseResponseInQueue:group];
}
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(#"finished with background for data fetch");
completionHandler(UIBackgroundFetchResultNewData);
}
This is one of my variants of background processing; not working well. This one supposed to wait a second and check if the input stream has data copied. If thats the case the parser would be called and the method comes to and end; removing one item from the dispatch group created in the iOS7 background app notification.
I don't like the dispatch_after as it seems very brute; but without I run in an endless loop as the streams seems not be triggered at all times.
Code:
- (void)parseResponseInQueue:(dispatch_group_t)group
{
if (gcdQueue != nil)
{
dispatch_group_async(group, gcdQueue, ^{
while ([self.data length] > 0)
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), gcdQueue, ^{
NSLog(#"%#, wait for parser in background", self.client.hostName);
});
}
#if 0
if ([self.data length] > 0)
{
NSLog(#"%#, start working on buffer %d from background", self.client.hostName, [self.data length]);
[self parseResponse];
NSLog(#"%#, finish working on buffer, left %d in background", self.client.hostName, [self.data length]);
// NSLog(#"data : %#", [[NSString alloc] initWithData:self.data encoding:NSUTF8StringEncoding]);
}
else
{
NSLog(#"%#, no data for background processing", self.client.hostName);
}
#endif
});
}
}
But somehow I don't get the refresh done. Sometimes the completionHandler finish without any update and sometime my dispatch group never finish.
So my question is mainly:
1) what is your suggestion to combine background app refresh with multiple streams in GCD queues.
2) does those runloops still be active when I trigger in background
3) are the GCD queues still active
4) should I better schedule in one runloop for all client connection in addition to one main runloop ?
Somehow I need to fresh thoughts on the way forward.
TIA
I have been working on a cross platform real time multiplayer game and ran into similar problems. The core issue I faced was with multiple threads competing for compute cycles and bottlenecks while trying to update common resources. I also had a bunch of scheduled threads that updated my game state.
I ended up switching to a single run loop for all my connections and background scheduled events. That helped in clearly identifying and properly isolating critical sections. This also simplified my control flow. I also saw an increase in performance since there were only 4 threads now that were competing for cycles instead of the earlier 20+
So for your Q4 I recommend that you switch to a single schedule.
For Q1 again, A single combined process for app refresh will not only speed up your app but make it easier to manage and debug.
For Q2 and Q3 It's best if you tested it out on your code by using logs as there may be factors involved that are not apparent in the code you shared.
I am having some heavy content to be loaded from data base and showed on to the Screen.
But at the same time there is a background task of 'plist to Data base' saving is going on.
Hence the UI gets stuck.
The code I am using as bellow,
For Data base fetch and UI update method
- (void)performBlockInBackground:(dispatch_block_t)taskBlock
completion:(dispatch_block_t)completionBlock
withPriotity:(dispatch_queue_priority_t)piority
{
__block dispatch_block_t taskBlockRef = taskBlock;
__block dispatch_block_t completionBlockRef = completionBlock;
dispatch_async(dispatch_get_global_queue(piority, 0), ^{
dispatch_sync(dispatch_get_global_queue(piority, 0), taskBlockRef);
dispatch_async(dispatch_get_main_queue(), completionBlockRef);
});
}
And I am calling the 'Plist to DB' method as
[self performSelectorInBackground:#selector(syncData) withObject:nil];
here the taskBlock is the heavy DB fetch and completionBlock is the UI Update.
If I call this method after some time (after the syncData method is done) then it updates smoothly. But if its not completed yet then the UI gets stuck.
It seems to be the issue of the thread deadlock but not clear on this
Please help !!
You don't need dispatch_sync() when you're already in the right queue. You can call the block like so: taskBlock().
- (void)performBlockInBackground:(dispatch_block_t)taskBlock
withPriority:(dispatch_queue_priority_t)priority
completionOnMainThread:(dispatch_block_t)completionBlock
{
dispatch_async(dispatch_get_global_queue(priority, 0), ^{
if (taskBlock) {
taskBlock();
}
if (completionBlock) {
dispatch_async(dispatch_get_main_queue(), completionBlock);
}
});
}
What happens is this:
Immediately send the work to a background thread. Now we aren't blocking the calling thread or the main thread.
If we have a taskBlock, do it now. TaskBlock is where you put your data fetch.
TaskBlock finishes.
If we have a completionBlock, send it to the main thread. CompletionBlock is where you update the UI.
I am writing an app that can revert the firmware of a particular device. While executing this revert code, I wish to display a progress indicator.
This problem is of course best tackled with the use of multiple threads (http://stackoverflow.com/questions/1225700/can-i-start-a-thread-by-pressing-a-button-in-a-cocoa-interface-and-keep-using-in).
I have implemented the performSelectorInBackground method, which (according to the documentation) launches the specified selector in a separate thread. Meanwhile, my GUI is updated from the main thread by querying the 'reverter' object.
However, the GUI does not seem to be updating until the code in the secondary thread has finished executing. I obviously need the two to run in parallel. Here is what I've got so far - I'd be really grateful for any help as this is my first time with threading.
-(IBAction)pushButton:(id)sender{
//instatiate reverter object, which does all the firmware processing
Reverter *reverter = [[Reverter alloc] init];
//update the GUI to show a tab with a progress indicator
[tabView selectTabViewItemWithIdentifier:#"RevertProgressTab"];
//process revert code in a separate thread
[reverter performSelectorInBackground:#selector(revertFirmware) withObject:nil];
//process is complete when reverter progress reaches 100
while (!([reverter progress] == 100)) {
//check for failure
if ([reverter hasFailed]) {
[self showRevertFailureTab:nil];
return;
}
//update the progress indicator in the interface
[revertProgressBar setDoubleValue:(double)[reverter progress]];
[NSThread sleepForTimeInterval:0.05];
}
[self showRevertSuccessTab:nil];
}
Have I done anything obvious that would stop the GUI from being updated while the revertFirmware method runs?
Your while loop
while (/*condition*/) {
[NSThread sleepForTimeInterval:x];
}
will prevent your UI from updating. Your UI will only update as soon as your pushButton: method returns.
Instead of polling I would advice you start using an asynchronous event model:
Add a delegate to your reverter object
#protocol ReverterDelegate <NSObject>
- (void) reverterProgressDidUpdate:(float)progress;
#end
#interface Reverter : NSObject {
id<ReverterDelegate> delegate;
}
#property(assign) id<ReverterDelegate> delegate;
#end
Register your controller class as a delegate to your reverter
reverter.delegate = self;
and handle that event
- (void) reverterProgressDidUpdate:(float)progress {
// update ui
}
In your background thread send out events to the main thread
- (void) revertFirmware {
// once in a while send notifications of progress updates
if ([self.delegate respondsToSelector:#selector(reverterProgressDidUpdate:)]) {
[self.delegate performSelectorOnMainThread:#selector(reverterProgressDidUpdate:) withObject:[NSNumber numerWithFloat:progress] waitUntilDone:NO];
}
}
Make sure you retain your reverter somewhere, and release it when it's done working. You are now leaking in your pushButton: method. Also this is just a suggestion towards a better model. Instead of using performSelectorInBackground you could take a look at NSOperation and NSOperationQueue for example.
I have a view that receives new data from a secondary thread. Every time it does, it should redraw itself. However, it doesn't play nice with the run loop, and after some time (it's non-deterministic), I end up getting <Error>: kCGErrorIllegalArgument: CGSUnionRegionWithRect : Invalid region messages in the console.
I'm not sure what's the right way to synchronize the calls to [view setNeedsDisplay:YES] across threads; can you help me?
To clarify a little, thread B (actually a dispatch queue) gives new contents to a view by calling this:
-(void)setImageBuffer:(unsigned char*)buffer
{
/* image handling stuff; thread-safe */
[self setNeedsDisplay:YES]; // but this is not thread-safe
}
And then thread A, on which runs the run loop, should redisplay the view.
-(void)setImageBuffer:(unsigned char*)buffer
{
/* image handling stuff; thread-safe */
[self performSelectorOnMainThread:#selector(induceRedraw)
withObject:nil
// Don't just copy this; pick one...
waitUntilDone:YES or NO];
}
-(void)induceRedraw
{
[self setNeedsDisplay:YES]; // but this is not thread-safe
}
With GCD you don't need the extra proxy method:
dispatch_queue_t q = dispatch_get_main_queue();
dispatch_async(q, ^(void) {
[self setNeedsDisplay: YES];
});