標籤:死機 假死 openfiledialog
我建立了一個開啟檔案的函數。代碼內容:
function Openfile { Add-Type -AssemblyName System.Windows.Forms $openfile = New-Object System.Windows.Forms.OpenFileDialog $openfile.Filter = ‘圖片|*.jpg|All Files|*.*‘ $openfile.InitialDirectory = "C:\ManageOperate\Staff Photo" [void]$openfile.ShowDialog() $Openfile.title="請選擇一張照片" $Openfile.FileName }
當在Win7SP1X64環境中調用的時候沒有問題。
650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201408/5/705126_1407233375kIc6.png" width="535" height="369" />
但當把指令碼放到Windows Server2008R2環境中卻出現了假死的問題。指令碼始終停在這裡沒有視窗彈出。
650) this.width=650;" title="image" style="border-right-width:0px;background-image:none;border-bottom-width:0px;padding-top:0px;padding-left:0px;padding-right:0px;border-top-width:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201408/5/705126_140723337550Dz.png" width="562" height="209" />
原來是由於指令碼運行在MTA也就是多線程模式導致的。Powershell2.0控制台預設是MTA,Powershell3.0 控制台預設是STA。
什麼是MTA和STA:
官方解釋:
STA:http://msdn.microsoft.com/en-us/library/ms680112(v=vs.85).aspx
MTA:http://msdn.microsoft.com/en-us/library/ms693421(v=vs.85).aspx
判斷Powershell的版本和線程模式 。
Win7SP1 X64
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201408/5/705126_1407233375TDyM.png" height="166" />
WinServer2008R2
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201408/5/705126_1407233375U1eq.png" height="201" />
解決方案:在代碼ShowDialog之前加入*.ShowHelp = $true判斷參數
function Openfile { Add-Type -AssemblyName System.Windows.Forms $openfile = New-Object System.Windows.Forms.OpenFileDialog $openfile.Filter = ‘圖片|*.jpg|All Files|*.*‘ $openfile.InitialDirectory = "C:\ManageOperate\Staff Photo" $openfile.ShowHelp=$true [void]$openfile.ShowDialog() $Openfile.title="請選擇一張照片" $Openfile.FileName }
我們再次在Server上執行這個函數。已經可以快顯視窗了。但視窗有點小呵呵。
650) this.width=650;" title="image" style="border-top:0px;border-right:0px;background-image:none;border-bottom:0px;padding-top:0px;padding-left:0px;border-left:0px;padding-right:0px;" border="0" alt="image" src="http://img1.51cto.com/attachment/201408/5/705126_14072333752YBJ.png" height="409" />
本文出自 “營運人生” 部落格,請務必保留此出處http://lixiaosong.blog.51cto.com/705126/1536071