网上有很多资料,对于自定义界面的NSIS安装包,可以采用BgWorker插件来获得多线程功能;
BgWorker插件下载地址:
http://nsis.sourceforge.net/BgWorker_plug-in
在制作安装包过程中发现,若将File命令,ExecWait等函数放入BgWorker中时,有小概率导致NSIS安装包崩溃。细查,发现BgWorker下载页面有如下说明:
This plugin calls the address stored in $0 in a low priority background thread, the “main” thread will process window messages while waiting for the worker thread.
Note that some internal functions and 3rd party components might not work in a separate thread!
翻译为:
请注意,某些内部函数和第三方组件可能不在单独的线程中工作!
说白了,就是利用BgWorker获得多线程的能力,是有可能不稳定的。那么是否BgWorker完全不可用呢?
我们可以将安装过程,File命令分为多个命令,尽量缩短每个命令的时间,在命令之间调用子进程Sleep函数。如下代码:
Function SleepFunc
Sleep 500
FunctionEnd
Function UpdateUI
GetFunctionAddress $0 SleepFunc
BgWorker::CallAndWait
FunctionEnd
; 设置输出路径
SetOutPath “$INSTDIR”
; 尝试覆盖
SetOverwrite try
File /r “install\image\*.*”
UpdateUI
File /r “install\resource\*.*”
UpdateUI
BgWorker::CallAndWait调用简单的Sleep函数是非常稳定的。借此可以达到刷新UI的功能。