在 VB.NET 中我可以执行这样的代码:
Sub Compute(sender As Object, e As EventArgs) Handle btnCompute.Click
For i = 0 to IterationLimit
' Do something
Applicaton.DoEvents()
Next
End Sub
一次迭代运行大约 50 毫秒,但整个方法运行大约 5 分钟。 “Appliction.DoEvents()”允许系统正确刷新表单并允许用户执行操作(按取消按钮、敲击热键等)。我如何才能像 Android 中的 java 那样?
最佳答案
如果您希望 UI 和其他组件在运行时保持响应,则需要在线程中运行代码。
请参阅此文档,了解如何从线程运行方法的非常简单明了的示例:https://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html
基本上你想扩展Thread
类:
public class MyThread extends Thread {
public void run() {
//do some background work
//put whatever you want your thread to do here
}
}
并这样调用它:
new MyThread().start();
关于java - 长时间作业时如何防止结冰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38338820/