在无限循环中启动脚本,并可以停止脚本 [英] Launch a script in an infinite loop with the possibility to stop it

查看:586
本文介绍了在无限循环中启动脚本,并可以停止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to develop a form from PowerShell that controls and monitor the execution of an script (in my case vbs but it could be any other) on an infinite loop.

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

I am new to PowerShell so I apology about the code structure. English is not my mother tong so I also apology about the mistakes.

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

I am using PowerShell because I want my form to have an icon and a menu in the Windows notify task bar. By now, the only thing I got is the icon, but I will get also the menu. This is not a problem.

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

I need my form, after Button Click, to launch a VisualBasic Script in an INFINITE loop every 3 seconds until I press again the button (or other button). But what I got is an infinite loop with no way of stop because once the button is pressed, the form gets freeze because it is inside the do while loop.

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

Does anyone know how can I solve my problem? the main purpose is to launch the same VBS every 3 seconds until I want it to stop...

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

As a test, the VisualBasic Script that I am trying to launch with my PowerShell program is a simple MsgBox that I called 'hello.vbs':

MsgBox "hello world"

为此,我创建了3个代码文件.它以一个名为"program.vbs"的VisualBasic脚本开头,该脚本调用PowerShell命令:

For doing so, I've created 3 code files. It starts with a VisualBasic Script named 'program.vbs' that calls PowerShell command:

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脚本:

Secondly it is executed a bat named 'program.bat' that calls PowerShell Script:

powershell -executionpolicy bypass -file "C:\Users\XXX\Desktop\Program\program.ps1"

最后,名为"program.ps1"的MAIN脚本使用控制和监视指令创建表单:

And finally the MAIN Script named 'program.ps1' creates the form with the control and monitor instructions:

[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

推荐答案

可能可以使用 Stop-Job /

Might you could use Start-Job. So your gui will not freeze while the job is running. You can stop / remove the job with Stop-Job / Remove-Job`.

这篇关于在无限循环中启动脚本,并可以停止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆