Bogdan Samchuk Bogdan Samchuk - 3 months ago 5x
Java Question

How to prevent freeze at long operations

In VB.NET i can execute code like this:

Sub Compute(sender Object, e EventArgs) Handle Compute.Click
For i = 0 to IterationLimit
' Do something
End Sub

One iteration run approximately 50 ms, but whole method run - about 5 minutes. "Appliction.DoEvents()" allow system to correctly refresh form and allow user to perform action (press button Cancel, stroke hotkey etc). How I can something like in java for Android?


You want to run the code in a thread if you want the UI and other components to remain responsive while it runs.

See this documentation for a very simple and clear example of how to run a method from a thread:

Basically you want to extend the Thread class:

public class MyThread extends Thread {
    public void run() {
        //do some background work
        //put whatever you want your thread to do here

And call it like this:

new MyThread().start();