在无限循环中启动脚本,有可能将其停止

问题描述 投票:0回答:1

我正在尝试从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”的蝙蝠,调用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")

Function Test() 
{
    do
    {
        Start-Sleep -Seconds 3
        Invoke-Expression "C:\Users\u36557\Desktop\Programita\hola.vbs"
    }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
                #####
                Test
                #####
            }else {
                $Form.Text = "Application stopped"
                $ComButt1.Text = "Launch application"
                $Label2.Text = 0
                $objNotifyIcon.Icon = $ProgIcon2
                $Form.Icon = $ProgIcon2
            }
        }
    )

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

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

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

Principal
winforms powershell loops
1个回答
0
投票

尝试一下:

Class Surrounding_Class
    Private Stop_Flag As Boolean = False

    Private Sub Btn_Handler(ByVal Sender As Object, ByVal e As EventArgs)
        Stop_Flag = Not Stop_Flag
        DoAction()
    End Sub

    Private Sub DoAction()
        Do
        Loop While Stop_Flag = False
    End Sub
End Class
© www.soinside.com 2019 - 2024. All rights reserved.