Powershell使用 OpenFileDialog類 假死問題解決。

來源:互聯網
上載者:User

標籤:死機   假死   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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.