powershell - PowerShell GUI / Windows 窗体导致脚本冻结
问题描述
以下是不起作用的代码的基本原理。
问题:
- API 请求需要很长时间才能完成,这反过来会导致 GUI 冻结和脚本挂起。
- 我可以手动运行呼叫,它工作正常,只是需要很长时间。
在引入此调用之前,我构建了其他所有内容,因此工作流程的其余部分正常工作。
function myFunc { try { $status.Text = 'Collecting data' $response = myHTTPCall -RequestTimeout 300 } catch { Write-Host "An error occurred:" Write-Host $_ } $status.Text = 'Done' } Add-Type -assembly System.Windows.Forms $main_form = New-Object System.Windows.Forms.Form $status = New-Object System.Windows.Forms.Label $status.Text = "" $main_form.Controls.Add($status) $Btn = New-Object System.Windows.Forms.Button $Btn.Add_Click({ myFunc }) $main_form.Controls.Add($Btn) $main_form.ShowDialog()