cocoa - Updating the progress indicator while downloading firmware to the device - Stack Overflow

admin2023-02-27  371

I am developing a cocoa application which downloads firmware to the device. The progress of downloading is showed using NSProgressIndicator. I call the -incrementBy: method of NSProgressIndicator after DeviceRequestTO method in a while loop. But the progress indicator gets updated only after the entire firmware is written to the device. It shows 100% completion at one go itself. So I added the -displayIfNeeded method of NSView class. Now it shows progress smoothly but this too occurs after the firmware download is complete. How can I achieve the progress indication and write operation simultaneously?

Following is the code:

while(1)
{
    int result = (*dev)->DeviceRequestTO(dev, &request);
    printf("\nBlocks Written Successfully: %d",DfuBlockCnt);
    [refToSelf performSelectorOnMainThread:@selector(notifyContent)
                        withObject:nil
                        waitUntilDone:NO];
}

//In main thread
- (void)notifyContent{
    [dnldIndicator incrementBy:1];
    [self displayIfNeeded];
}

Answers:

I am developing a cocoa application which downloads firmware to the device. The progress of downloading is showed using NSProgressIndicator. I call the -incrementBy: method of NSProgressIndicator after DeviceRequestTO method in a while loop. But the progress indicator gets updated only after the entire firmware is written to the device. It shows 100% completion at one go itself. So I added the -displayIfNeeded method of NSView class. Now it shows progress smoothly but this too occurs after the firmware download is complete. How can I achieve the progress indication and write operation simultaneously?

Following is the code:

while(1)
{
    int result = (*dev)->DeviceRequestTO(dev, &request);
    printf("\nBlocks Written Successfully: %d",DfuBlockCnt);
    [refToSelf performSelectorOnMainThread:@selector(notifyContent)
                        withObject:nil
                        waitUntilDone:NO];
}

//In main thread
- (void)notifyContent{
    [dnldIndicator incrementBy:1];
    [self displayIfNeeded];
}

Answers:

The method you need to call is setNeedsDisplay:, not displayIfNeeded. The latter means “send yourself display if somebody has sent you setNeedsDisplay:YES”. If you don't do that last part, the view doesn't know it should display, and displayIfNeeded will do nothing.

And once you add the setNeedsDisplay: message, you may be able to cut out the displayIfNeeded message, as the framework sends that message to the window (and, hence, to all its views) periodically anyway.

Answers:

Your code looks exactly like some that I use for updating UIProgressIndicators and NSProgressIndicators on the Mac and iPhone, code that works perfectly for me. I'm assuming, like menumachine, that your while loop exists on a background thread (created using performSelectorInBackground:withObject: or NSThread's detachNewThreadSelector:toTarget:withObject:).

Are the minValue and maxValue of the progress indicator set correctly (0 and 100 or whatever your scale is)?

How frequently do updates occur? Maybe you're sending too many events too quickly and the UI is not having a chance to update properly.

This code should work, as far as I can tell.

转载请注明原文地址:https://www.jcdi.cn/read-201033.html
New Post(0)