winforms - 在无限循环中启动脚本并可能停止脚本

标签 winforms powershell loops

我正在尝试从PowerShell开发一种表单,该表单在无限循环中控制和监视脚本(在我的情况下为vbs,但可以是其他任何脚本)的执行。

我是PowerShell的新手,所以我对代码结构表示歉意。英语不是我的母语,所以我也对错误表示歉意。

我使用PowerShell是因为我希望我的表单在Windows通知任务栏中有一个图标和一个菜单。到目前为止,我唯一获得的是图标,但我还将获得菜单。这不是问题。

在单击按钮后,我需要我的表单以每3秒在INFINITE循环中启动一个VisualBasic脚本,直到再次按下按钮(或其他按钮)为止。 但是我得到的是一个无止境的无限循环,因为一旦按下按钮,表单就会冻结,因为它位于do while循环中。

有谁知道我该如何解决我的问题?主要目的是每3秒启动相同的VBS,直到我希望它停止...

作为测试,我尝试通过PowerShell程序启动的VisualBasic脚本是一个简单的MsgBox,我将其称为“hello.vbs”:

MsgBox "hello world"

为此,我创建了3个代码文件。它以一个名为“program.vbs”的VisualBasic脚本开头,该脚本调用PowerShell命令:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\XXX\Desktop\Program\program.bat" & Chr(34), 0
Set WshShell = Nothing

其次,执行一个名为“program.bat”的bat,该bat调用PowerShell脚本:
powershell -executionpolicy bypass -file "C:\Users\XXX\Desktop\Program\program.ps1"

最后,名为“program.ps1”的MAIN脚本使用控制和监视指令创建表单:
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

$functions = {
    function Test() 
    {
        do
        {
            Invoke-Expression "C:\Users\XXX\Desktop\Programita\hello.vbs"
            Start-Sleep -Seconds 2
        }while ($Label2.Text -ne 0)
    }
}

Function Principal {
    $ProgIcon1 = "C:\Users\XXX\Desktop\Program\icon1.ico"
    $ProgIcon2 = "C:\Users\XXX\Desktop\Program\icon2.ico"
    $Form = New-Object System.Windows.Forms.Form
    $ContextMenuProg = New-Object System.Windows.Forms.ContextMenu
    $Menu = New-Object System.Windows.Forms.MenuItem
    $Icon1 = New-Object System.Drawing.Icon ($ProgIcon1)
    $Icon2 = New-Object System.Drawing.Icon ($ProgIcon2)
    $Label1 = New-Object System.Windows.Forms.Label
    $Label2 = New-Object System.Windows.Forms.Label
    $ComButt1 = New-Object System.Windows.Forms.Button
    $ComButt2 = New-Object System.Windows.Forms.Button
    $objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon

    $Form.Text = "Application stopped"

    $Form.Controls.Add($Label1)
    $Form.Controls.Add($Label2)
    $Form.Controls.Add($ComButt1)
    $Form.Controls.Add($ComButt2)

    $Form.Width = 260
    $Form.Height = 180
    $Form.FormBorderStyle = "FixedSingle"
    $Form.MaximizeBox = $False
    $Form.StartPosition = "CenterScreen"
    $Form.Icon = $Icon2

    $Label1.Text = "My first form with icon inside task bar"
    $Label1.Left = 30
    $Label1.Top = 20
    $Label1.AutoSize = $True

    $ComButt1.Text = "Start application"
    $ComButt1.Left = 12
    $ComButt1.Top = 100
    $ComButt1.Width = 102

    $ComButt2.Text = "Close application"
    $ComButt2.Left = 124
    $ComButt2.Top = 100
    $ComButt2.Width = 102
    $ComButt2.Add_Click({
        [System.Windows.Forms.DialogResult]::Cancel
    })
    $ComButt2.DialogResult = [System.Windows.Forms.DialogResult]::Cancel

    $Label2.Text=0
    $ComButt1.Add_Click(
        {
            if ($Label2.Text -eq 0)
            {
                $Form.Text = "Active application"
                $ComButt1.Text = "Application paused"
                $Label2.Text = 1
                $objNotifyIcon.Icon = $ProgIcon1
                $Form.Icon = $ProgIcon1
                #####
                Start-Job -InitializationScript $functions -ScriptBlock {Test} -Name MyJob
                #####
            }else {
                $Form.Text = "Application stopped"
                $ComButt1.Text = "Launch application"
                $Label2.Text = 0
                $objNotifyIcon.Icon = $ProgIcon2
                $Form.Icon = $ProgIcon2
                Remove-Job -Name MyJob -Force
            }
        }
    )

    $Label2.Left = 30
    $Label2.Top = 50
    $Label2.AutoSize = $True

    $objNotifyIcon.Icon = $ProgIcon2
    $objNotifyIcon.ContextMenu = $ContextMenuProg

    $objNotifyIcon.Visible = $True
    $Form.ShowDialog()
    $objNotifyIcon.Visible = $False
}

Principal

最佳答案

可能您可以使用 Start-Job 。因此,在作业运行时,您的GUI不会冻结。您可以使用 Stop-Job / Remove-Job `停止/删除作业。

关于winforms - 在无限循环中启动脚本并可能停止脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58536365/

相关文章:

c# - 区分用户更改 Checkbox.Checked 值,还是以编程方式更改

c# - 为什么切换我的表单的可见性会禁用我的 KeyDown 事件?

java - Android Activity会关闭Looper&Handler吗?

c# - 事件处理程序中的 "reentrant call to SetCurrentCellAddressCore"- 仅当单元格行索引和列索引相等时

c# - 如何将 MySQL 数据库中的特定列值显示到 DataGridView 中

c# - 使标 checkout 现和消失的计时器非常缓慢且无响应

php - 循环中的错误数组值

powershell - Invoke-RestMethod:发送到 header 时,无法根据请求找到身份验证数据

powershell - powershell 将文件移动到 Amazon s3

json - Powershell Invoke-RestMethod 响应不完整