小编DJ *_*res的帖子

使用Windows窗体在脚本终止后锁定PowerShell ISE分钟

我这里有一个有趣的问题.我正在创建一个日历选择器,供我们创建帐户时使用.它工作正常并且仍在进行中但是我注意到当我在powershell ISE中运行脚本时,几分钟后它就会锁定(我能够在此之前编辑并保存代码几分钟).事件日志中没有任何内容.我得到一个对话框,说PowerShell没有响应.内存使用情况也很正常.我不知道发生了什么.

无论我如何运行Powershell ISE(以管理员身份运行,作为另一个帐户运行,以及正常的ISE),我都会运行Windows 8.1.

同事建议它可能是公寓型号,所以我尝试了STA和MTA,但问题出现了.从控制台主机运行相同的代码时不会发生这种情况.

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 

$objForm = New-Object Windows.Forms.Form 

$objForm.Text = "Select a Date" 
$objForm.Size = New-Object Drawing.Size @(490,250)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Enter") 
        {
            $script:dtmDate=$objCalendar.SelectionStart
            $objForm.Close()
        }
    })

$objForm.Add_KeyDown({
    if ($_.KeyCode -eq "Escape") 
        {
            $objForm.Close()
        }
    })

$objCalendar = New-Object System.Windows.Forms.MonthCalendar 
$objCalendar.Text = "Start"
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar) 

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})  
[void] $objForm.ShowDialog() 

if ($dtmDate)
    {
        Write-Host "Date selected: $dtmDate"
    }

$objForm.Dispose()
Run Code Online (Sandbox Code Playgroud)

回应@The Unique …

powershell winforms powershell-4.0

19
推荐指数
1
解决办法
4557
查看次数

标签 统计

powershell ×1

powershell-4.0 ×1

winforms ×1