简体   繁体   English

是否可以在PowerShell中同时运行2个功能?

[英]Is it possible to run 2 functions at the same time in PowerShell?

In a PowerShell script I have 在PowerShell脚本中,我有

function f1{ do something}
function f2{ do something else}
button1
button2
richtextbox

When I press button1 it runs f1 and it outputs data in the richtextbox . 当我按下button1它将运行f1并在richtextbox输出数据。 When I press button2 I need the script to run f2 even if f1 hasn't finished. 当我按下button2即使f1尚未完成,我也需要脚本来运行f2 The data from f2 I don't need it to be outputted in the same richtextbox , it can be a different one. 来自f2的数据我不需要将其输出到同一richtextbox ,它可以是不同的。 My question is: can 2 different functions run at the same time in PowerShell? 我的问题是:可以在PowerShell中同时运行2个不同的功能吗?

Update: For my PowerShell script I've done a GUI that has a bunch of button that do different things. 更新:对于我的PowerShell脚本,我完成了一个GUI,该GUI具有一堆可以执行不同操作的按钮。 When I press one button it starts to connect to a serial device and outputs data to the richtextbox and at some points it saves data into a text file. 当我按下一个按钮时,它开始连接到串行设备并将数据输出到richtextbox,并在某些时候将数据保存到文本文件中。 2 other buttons have the following functions: it takes parts of data from the text file saved, opens a temporary php web server, opens a link to create a pdf file, and the it send the file to the correct printer/tray for printing. 其他2个按钮具有以下功能:它从保存的文本文件中获取部分数据,打开一个临时php Web服务器,打开一个链接以创建pdf文件,然后它将文件发送到正确的打印机/托盘进行打印。 These 2 buttons I've just noticed that they work. 我刚刚注意到这两个按钮可以正常工作。 My problem is with the buttons that are in a popup form. 我的问题是使用弹出形式的按钮。 One other button from my main form opens a popup form and populates it with data from the text file saved above. 我的主表单中的另一个按钮打开了一个弹出表单,并使用上面保存的文本文件中的数据填充它。

$print_equip_button.Add_Click({
    print "link.php?data=$data" $file_location $type
}.GetNewClosure())

In the main form the code above works, from popup it doesn't. 在主要形式中,上面的代码有效,但从弹出窗口无效。 Also tried with no luck. 也尝试没有运气。

$print_equip_button.Add_Click({
    Start-Job -ScriptBlock {print "link.php?data=$data" $file_location $type}
}.GetNewClosure())

I've added at the beginning of a function 我在函数的开头添加了

for($i=1;$i -le 100;$i++){
    [System.Windows.Forms.Application]::DoEvents()
    start-sleep -s 1
    $LogTextBox.Text = "$temp`n" + "Count $i"
}

to see if what happens to function f1 when I press a button in the popup and from what I can see, when I open the popup form, the counting stops and continues when I close it. 看看当我按下弹出窗口中的一个按钮时函数f1是否会发生变化,从我所看到的情况中,当我打开弹出窗口时,计数会停止,并在关闭时继续。

UPDATE: I found that if I have the script opened in PowerShell ISE and run it from there, everything works as I wanted without the need of "Start-Job". 更新:我发现,如果我在PowerShell ISE中打开了脚本并从那里运行它,那么一切都会按我的意愿进行,而无需“启动作业”。 When running the .ps file on its own it doesn't work, not even with "Start-Job" 单独运行.ps文件时,即使使用“开始作业”,该文件也无法正常工作

Both jobs and runspaces will do this for you, remember though you have to specifically get the information back from the function, just running it in a different thread won't give you a return, as the output will be in a separate thread. 作业和运行空间都将为您执行此操作,请记住,尽管您必须从函数中专门获取信息,但仅在不同的线程中运行它不会返回任何信息,因为输出将在单独的线程中。

Jobs are better for a single function that takes a long time eg deploying an ovf and runspaces are better for more broad functions eg pinging 1000 machines in parallel. 作业对于需要较长时间的单个功能(例如,部署ovf)更好,而运行空间对于更广泛的功能(例如,并行ping 1000台计算机)则更好。

You can use receive-job to get results back from start-job. 您可以使用receive-job从start-job获取结果。

you can do this by asynchronous ui updation using runspaces: 您可以使用运行空间通过异步ui更新来做到这一点:

  $button1_Click={

$ps = [powershell]::create()
$ps.AddScript(
     {
     #replace with your function definition
       while($true){
        $richtextbox1.appendtext("1")
        start-sleep 1
        }
    }
)

$ps.Runspace.SessionStateProxy.SetVariable("richtextbox1", $richtextbox1)
$ps.BeginInvoke()
}


$button2_Click={
$ps2 = [powershell]::create()
$ps2.AddScript(
     {
      #replace with your function definition
       while($true){
        $richtextbox2.appendtext("1")
        start-sleep 1
        }
    }
)


$ps2.Runspace.SessionStateProxy.SetVariable("richtextbox2", $richtextbox2)
$ps2.BeginInvoke()


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM