How does performing time-consuming operations on the main thread affect the UI refresh

Related searches

Synchronization time on the main thread can cause the main thread to block. Is this the cause of UI lag? If I'm performing asynchronous, time-consuming operations on the main thread, does this affect the flow of the UI on the main thread? Again, time-consuming operations must be placed in child thread asynchronous operations.

  1. Is the view refresh code waiting for a time-consuming operation to complete before it can be executed the reason for the UI not flowing?

    [NSThread detachNewThreadSelector:@selector(syncMain) toTarget:self withObject:nil];
    - (void)syncMain {
        dispatch_queue_t queue = dispatch_get_main_queue();
    
        dispatch_sync(queue, ^{
            // 追加任务1
            for (int i = 0; i < 2; ++i) {
                [NSThread sleepForTimeInterval:2];   // Simulated time-consuming operation
                NSLog(@"1---%@",[NSThread currentThread]);      // on the main thread
            }
        });
        self.testImage=[UIImage imageNamed:xxx];//Waiting for a time-consuming operation to complete, causing a UI refresh block
    }
    
  2. Does performing asynchronous time-consuming operations on the main thread affect UI fluidity?

    - (void)asyncMain {    
        dispatch_queue_t queue = dispatch_get_main_queue();
    
        dispatch_async(queue, ^{
            // 追加任务1
            for (int i = 0; i < 2; ++i) {
                [NSThread sleepForTimeInterval:2]; // Simulated time-consuming operation
                NSLog(@"1---%@",[NSThread currentThread]);      // on the main thread,too
            }
        });
    
        self.testImage=[UIImage imageNamed:xxx];//Execute code now
    }
    
  3. Whether time-consuming operations can only be performed asynchronously on child threads to keep the UI intact?

    - (void)asyncChild {    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                       [NSThread sleepForTimeInterval:2]; // Simulated time-consuming operation
                    dispatch_async(dispatch_get_main_queue(), ^{
                    self.testImage=[UIImage imageNamed:xxx];
                        });
                });
    }
    

Can you give an example of some operations you are having issues with?

If the main thread is busy doing other things instead of UI releated things, then yes, you might experience a slow, or unusable, UI.

As a general rule, time consuming, or synchronous, operations should be performed on background threads to avoid this issue. Please note, however, that UI/UIKit related operations must be performed on the main thread, or your app might crash.

Better performance through threading, Performing network operations Making adept use of threads on Android can help you boost your This main thread, also known as the UI thread, is responsible for this goal, the UI/View hierarchy must update on the main thread . of that object at the same time that any other thread is referencing the� To move data from a background thread to the UI thread, use a Handler that's running on the UI thread. Define a handler on the UI thread. Handler is part of the Android system's framework for managing threads. A Handler object receives messages and runs code to handle the messages. Normally, you create a Handler for a new thread, but you can

If you are on the main thread and dispatch something to an asynchronous thread it will not have an impact on the UI unless the thread is consuming all the resources on the device.

What will get you though is when your async thread is done and you go to update the UI with the results of the async operation. If you forget to move the UI updates onto the main thread the UI will update at some random time interval, maybe, or it won't update at all. This is what gets most people when they do async operations.

Processes and threads overview, As such, the main thread is also sometimes called the UI thread. Specifically, if everything is happening in the UI thread, performing long operations such as However, note that you cannot update the UI from any thread other than the UI thread or the "main" thread. a potentially time consuming task AsyncTask. AsyncTask enables the proper and easy use of the UI thread. This class allows you to perform background operations and publish results on the UI thread without the use of threads or

iOS: Why the UI need to be updated on Main Thread, But think it carefully, why do we have to update the UI in the main thread? be unprocessable, which in turn affects performance, resulting in the inability clever way to put some time-consuming operations asynchronously,� Isolates. At the point when Dart starts, there will be one main Isolate(Thread).It is the original main executing thread of the application, alluded to as the UI Thread.

Asynchronous Operations in Swing, This article looks thread management in a Swing GUI. This article looks at how to manage threads in Swing, keeping these long-running operations from affecting the UI. Rule #2: do not run time-consuming operations on the event thread. To me this is the most important rule of all, and is the main point of this article. A different solution is to avoid blocking the main event loop by performing long operations in a separate thread. This is especially useful if the task is performed by a third party library in a blocking fashion. In such a situation it might not be possible to interrupt it to let the GUI process pending events.

Multithreading and Concurrency - Java Programming Tutorial, A multitasking system can perform many tasks concurrently by sharing the of an I/O operation), another thread can use the CPU time to perform computations, A standalone Java application starts with a single thread (called main thread) The infamous Unresponsive User Interface (UI) problem is best illustrated by the� Time-consuming operations like downloads and database transactions can cause your user interface (UI) to seem as though it has stopped responding while they are running. When you want a responsive UI and you are faced with long delays associated with such operations, the BackgroundWorker class provides a convenient solution.

Such cases occur when the long-running operation is executed on the same thread as the RadWaitingBar waiting process: the primary UI Thread. The operation does not allow the form to update its UI and as a result the control does not perform any waiting animation. One obvious solution is to start the time-consuming operation in a new thread.

Comments
  • Thanks for providing examples. That’s extremely helpful.
  • Sorry, now I've given the example