我有一个使用异步运行空间的脚本,以便在后台处理内容时显示XAML / WPF GUI。当我单击按钮时,它会崩溃,但只有在特定状态下才会崩溃。让我详细说明一下。
该脚本首先ping一长串服务器以确保它们已启动。之后,它会运行一些SQL查询,WMI调用,其他逻辑,处理所有数据,然后在另一个窗口中一次一行地使用信息更新我的DataGrid。
按钮只显示WinForm确认:
#$Global:uihash is my synchronized hashtable.
$Global:uihash.Button.add_click({if([System.Windows.Forms.MessageBox]::Show `
("Yes or No?", "Question",[System.Windows.Forms.MessageBoxButtons]::OKCancel) -eq "OK")
{Do stuff back in main PS Window}
如果我在脚本ping所有设备时按下按钮,它可以正常工作。如果我在脚本期间的任何其他时间单击按钮,PS Windows会立即翻转而无法响应,我必须将其杀死。
从应用程序事件日志:
程序PowerShell_ISE.exe版本10.0.10586.117已停止 与Windows交互并关闭。
我喜欢95%肯定是因为ping设备是脚本中唯一与其他窗口完全没有交互的部分,我只是不知道如何解决这个问题,甚至不知道如何处理它。
我想知道是否有人曾经看过或经历过这种或类似的东西,以及你是如何解决它的。任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我从未能完全理解这一点,但我确实为我找到了一个好的解决方法。我将脚本的主要部分放在Do-Until
循环中,然后单击按钮更改我创建的全局变量。这使它脱离了循环,关闭了窗口,让所有的完成任务运行而不会崩溃。
#$Global:uihash is my synchronized hashtable.
#Create synchronized hash table variable
$Global:uiHash.Add(“End”,$false)
#Create Click Event
$Global:uihash.Button.add_click({if([System.Windows.Forms.MessageBox]::Show
("Yes or No?", "Question",[System.Windows.Forms.MessageBoxButtons]::OKCancel) -eq "OK")
{$Global:uiHash.End = $true}
Do {A whole bunch of stuff}
Until ($global:uiHash.end -eq $true)
#Close the window
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.Window.close() },"Normal")
#Then process the rest of the script