Skip to content

Commit

Permalink
尝试修复独立进程启动时更新后卡死不能退出的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
asforest committed Jul 16, 2023
1 parent 1ecb321 commit 2b2280b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
47 changes: 28 additions & 19 deletions src/main/kotlin/mcpatch/McPatchClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,21 @@ class McPatchClient

// 将更新任务单独分进一个线程执行,方便随时打断线程
var ex: Throwable? = null
val workthread = WorkThread(window, options, updateDir, progDir)
workthread.isDaemon = true
workthread.setUncaughtExceptionHandler { _, e -> ex = e }

if (!options.quietMode)
window?.show()

window?.titleTextSuffix = ""
window?.titleText = Localization[LangNodes.window_title]
window?.labelText = Localization[LangNodes.connecting_message]
window?.onWindowClosing?.once { win ->
win.hide()
if (workthread.isAlive)
workthread.interrupt()
}

workthread.start()
workthread.join()
val workthread = WorkThread(window, options, updateDir, progDir)

try {
workthread.run()
} catch (e: Exception) {
ex = e
}

window?.destroy()

Expand All @@ -118,8 +115,8 @@ class McPatchClient
ex !is ClosedByInterruptException)
{
try {
Log.error(ex!!.javaClass.name)
Log.error(ex!!.stackTraceToString())
Log.error(ex.javaClass.name)
Log.error(ex.stackTraceToString())
} catch (e: Exception) {
println("------------------------")
println(e.javaClass.name)
Expand All @@ -129,8 +126,8 @@ class McPatchClient
if (graphicsMode)
{
val appVersion = "${Environment.Version} (${Environment.GitCommit})"
val className = if (ex!! !is BaseException) ex!!.javaClass.name + "\n" else ""
val errMessage = MiscUtils.stringBreak(className + (ex!!.message ?: "<No Exception Message>"), 80)
val className = if (ex !is BaseException) ex.javaClass.name + "\n" else ""
val errMessage = MiscUtils.stringBreak(className + (ex.message ?: "<No Exception Message>"), 80)
val title = "发生错误 $appVersion"
var content = errMessage + "\n"
content += if (!hasStandaloneProgress) "点击\"\"显示错误详情并停止启动Minecraft," else "点击\"\"显示错误详情并退出,"
Expand All @@ -141,19 +138,19 @@ class McPatchClient
{
if (choice)
{
DialogUtils.error("错误详情 $appVersion", ex!!.stackTraceToString())
throw ex!!
DialogUtils.error("错误详情 $appVersion", ex.stackTraceToString())
throw ex
}
} else {
if (choice)
DialogUtils.error("错误详情 $appVersion", ex!!.stackTraceToString())
throw ex!!
DialogUtils.error("错误详情 $appVersion", ex.stackTraceToString())
throw ex
}
} else {
if (options.noThrowing)
println("文件更新失败!但因为设置了no-throwing参数,游戏仍会继续运行!\n\n\n")
else
throw ex!!
throw ex
}
} else {
Log.info("updating thread interrupted by user")
Expand Down Expand Up @@ -183,6 +180,18 @@ class McPatchClient
throw e
} finally {
Log.info("RAM: " + MiscUtils.convertBytes(Runtime.getRuntime().usedMemory()))

// 打印调用堆栈
for ((thread, frames) in Thread.getAllStackTraces()) {
Log.info("Thread #${thread.id}: ${thread.name}: (daemon: ${thread.isDaemon})")
for (frame in frames) {
Log.info(" $frame")
}
Log.info("")

if (hasStandaloneProgress && !thread.isDaemon)
thread.interrupt()
}
}

return false
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/mcpatch/WorkThread.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class WorkThread(
val options: GlobalOptions,
val updateDir: File2,
val progDir: File2,
): Thread() {
) {
var downloadedVersionCount = 0

/**
* McPatch工作线程
*/
override fun run()
fun run()
{
if (!options.quietMode)
window?.show()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/mcpatch/data/GlobalOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ data class GlobalOptions(
concurrentThreads = getOption(map, "concurrent-threads") ?: 4,
concurrentBlockSize = getOption(map, "concurrent-block-size") ?: 4194304,
versionsFileName = getOption(map, "server-versions-file-name") ?: "versions.txt",
ignoreHttpsCertificate = getOption(map, "ignore-https-certificate") ?: true
ignoreHttpsCertificate = getOption(map, "ignore-https-certificate") ?: false
)
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/mcpatch/gui/ChangeLogs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,19 @@ class ChangeLogs
changlogs.lineWrap = true
closeButton.addActionListener { close() }

threadLock.isDaemon = true
threadLock.start()

Thread {
Thread.sleep(1000)
this.window.repaint()
}.start()
}.also { it.isDaemon = true }.start()
}

fun setAutoClose(time: Int)
{
autoCloseDelay = time
autoCloseThread.isDaemon = true
autoCloseThread.start()
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/mcpatch/gui/McPatchWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class McPatchWindow(width: Int = 300, height: Int = 120)
private var label = JLabel("空标签空标签空标签空标签空标签空标签空标签空标签空标签").apply { setBounds(5, 10, 275, 20); horizontalAlignment = JLabel.CENTER; window.contentPane.add(this) }
private var progressBar = JProgressBar(0, 1000).apply { setBounds(10, 40, 265, 30); isStringPainted = true; window.contentPane.add(this) }

val onWindowClosing = Event<McPatchWindow>()
// val onWindowClosing = Event<McPatchWindow>()

init {
window.isUndecorated = false
Expand All @@ -27,11 +27,11 @@ class McPatchWindow(width: Int = 300, height: Int = 120)
window.isResizable = false
// window.isAlwaysOnTop = true

window.addWindowListener(object : WindowAdapter() {
override fun windowClosing(e: WindowEvent?) {
onWindowClosing.invoke(this@McPatchWindow)
}
})
// window.addWindowListener(object : WindowAdapter() {
// override fun windowClosing(e: WindowEvent?) {
// onWindowClosing.invoke(this@McPatchWindow)
// }
// })
}

/**
Expand Down

0 comments on commit 2b2280b

Please sign in to comment.