首页 \ 问答 \ GetNewClosure和Start-Job(GetNewClosure and Start-Job)

GetNewClosure和Start-Job(GetNewClosure and Start-Job)

我无法理解GetNewClosure如何与Start-Job一起工作。 例如,我有以下代码

function Test([string]$Name)
{
     $block = { Write-Host "Name = $Name" }.GetNewClosure()
     &$block
     Other $block
     OtherJob $block
}

function Other([scriptblock]$Block)
{
    Write-Host -NoNewline "In Other: "
    &$Block
}

function OtherJob([scriptblock]$Block)
{
    Write-Host -NoNewline "In OtherJob: "
    $j = Start-Job -ScriptBLock $Block
    Start-Sleep -s 1
    $j | Receive-Job
}

在调用我得到的代码时

PS C:\> Test "foo"
Name = foo
In Other: Name = foo
In OtherJob: Name = 

请注意,在OtherJob中未捕获$ Name。

它可能与Start-Job启动一个新的PS实例有关,但是有没有解决方法(最好不包括使用-ArgumentList)?

PS:版本表应该重要

PS C:\> $PSVersionTable

Name                           Value                                                                                    
----                           -----                                                                                    
CLRVersion                     2.0.50727.5485                                                                           
BuildVersion                   6.1.7601.17514                                                                           
PSVersion                      2.0                                                                                      
WSManStackVersion              2.0                                                                                      
PSCompatibleVersions           {1.0, 2.0}                                                                               
SerializationVersion           1.1.0.1                                                                                  
PSRemotingProtocolVersion      2.1 

I'm having trouble understanding how GetNewClosure works in conjunction with Start-Job. Case in point I have the following code

function Test([string]$Name)
{
     $block = { Write-Host "Name = $Name" }.GetNewClosure()
     &$block
     Other $block
     OtherJob $block
}

function Other([scriptblock]$Block)
{
    Write-Host -NoNewline "In Other: "
    &$Block
}

function OtherJob([scriptblock]$Block)
{
    Write-Host -NoNewline "In OtherJob: "
    $j = Start-Job -ScriptBLock $Block
    Start-Sleep -s 1
    $j | Receive-Job
}

When calling the code I get

PS C:\> Test "foo"
Name = foo
In Other: Name = foo
In OtherJob: Name = 

Notice that $Name is not captured in OtherJob.

It is probably related to Start-Job starting a new PS instance, but is there any workaround for this (Preferably one that does not include using -ArgumentList)?

PS: The version table should it matter

PS C:\> $PSVersionTable

Name                           Value                                                                                    
----                           -----                                                                                    
CLRVersion                     2.0.50727.5485                                                                           
BuildVersion                   6.1.7601.17514                                                                           
PSVersion                      2.0                                                                                      
WSManStackVersion              2.0                                                                                      
PSCompatibleVersions           {1.0, 2.0}                                                                               
SerializationVersion           1.1.0.1                                                                                  
PSRemotingProtocolVersion      2.1 

原文:https://stackoverflow.com/questions/33000868
更新时间:2024-02-05 22:02

最满意答案

如果您专门处理发布/订阅方案,我建议使用ZeromMQ (clrzmq + libzmq)。 ZeroMQ非常易于使用且速度非常快。 你会发现很多C#例子,对应于ZeroMQ网站上的各种场景。 根据您的具体需求,您可以将ZeroMQ和OpenPGM结合起来进行pgm(tcp)或epgm(udp)多播。


If you are specifically dealing with a publish/subscribe scenario, I would advise using ZeromMQ (clrzmq + libzmq). ZeroMQ is extremely easy to use and very fast. You'll find plenty of C# examples, corresponding to various scenarios on the ZeroMQ site. Depending on your specific needs, you can combine ZeroMQ and OpenPGM to do pgm (tcp) or epgm (udp) multicasting.

相关问答

更多
  • 如果您需要这个用于开发目的,我可以推荐单元测试。 在我使用单元测试之前,我会像你描述的那样开发:创建一个控制台服务器和客户端,并与控制台客户端交互,以查看发送和接收的数据是否有意义。 我现在用单元测试做的是,我创建的小测试与我通过客户端控制台手动完成的操作大致相同。 单元测试方法具有以下优点: 测试小而清晰,所以我知道我在测试什么; 测试是可重复的,所以我知道当所有测试通过时,我之前测试过的小片仍然有效; 测试很容易运行,就像控制台客户端一样简单; 当我创建了足够的测试时,我可以在我无法与控制台交互的应用程 ...
  • 您当前在具有相同TCP连接的同一端口上启动一组Listener实例。 这不会像你想象的那样(你已经注意到了)。 诀窍是有一个打开Socket并接受客户端。 当客户端被接受时,您会生成一个处理通信的新线程。 基本方案是: 创建Socket 调用BeginAcceptSocket 在BeginAcceptSocket回调中,为连接的客户端生成一个新线程 再次调用BeginAcceptSocket以侦听新客户端 You currently start a set of Listener instances on ...
  • 关于并发性问题,您的应用程序应设计为尽可能缩短与数据库的连接。 数据库上的每个操作都应该包括:打开连接,对数据库执行操作,关闭连接而不是:打开连接,执行一系列可能与获取/更新/插入数据相关的工作,然后在“最后“关闭连接。 现在,关于应用程序并发性,最终会出现两种情况。 在方案一中,我将其称为“最后写入获胜”,无论最后写入给定行的连接是存储的数据的版本。 如果Alice然后Bob同时写入同一行的Name列,Bob的版本将是存储的。 这是迄今为止最简单的,但如果您可能有很多人更新相同的数据,则可能会出现问题。 ...
  • 啊,更多的阅读帮助我搞清楚了。 Flux主要是客户端应用程序模式。 下图说明了典型的用例,以及服务器及其关联状态如何与客户端Flux逻辑断开连接。 换句话说,客户端上的Flux无法解决在web-api端管理状态和组件的问题。 对于与服务器端代码紧密耦合的客户端应用程序(如电子应用程序,iPython笔记本,NW.js应用程序),实现类似于Cocoa委托模式的调度程序而不是UI线程可能是有意义的。 Ah, some more reading helped me figure it out. Flux was ...
  • 没有为你的具体要求进行太多的细节,我肯定会看WCF 。 它包含了许多现有的远程处理,客户端/服务器,Web服务场景,并且具有非常完整和安全的框架。 使用WCF的客户端服务器编程 Without going into too much detail for your specific requirements, I would definitely look at WCF. It encompasses a lot of the existing remoting, client / server, web ...
  • 我在搜索Google时找到了一些转换器,希望您对此有所帮助: C ++到C#转换器| 免费开发软件在SourceForge.net下载 C ++到C#转换器 谢谢, 我希望你觉得这有用 :) There are a few converters I've found while searching with Google which I hope you find helpful: C++ to C# Translator | Free Development software downloads at S ...
  • 如果服务托管在同一个进程中,您可以在内存中(或在必要时在持久存储中)使用一些本地字典( Dictionary ),并通过共享(静态)类访问它。 你需要比这更复杂的东西吗? If the services are hosted in the same process, you can simply use some local dictionary (Dictionary) in memory (or in a persistent st ...
  • 如果您专门处理发布/订阅方案,我建议使用ZeromMQ (clrzmq + libzmq)。 ZeroMQ非常易于使用且速度非常快。 你会发现很多C#例子,对应于ZeroMQ网站上的各种场景。 根据您的具体需求,您可以将ZeroMQ和OpenPGM结合起来进行pgm(tcp)或epgm(udp)多播。 If you are specifically dealing with a publish/subscribe scenario, I would advise using ZeromMQ (clrzmq ...
  • 好吧,总有JSON。 它应该在两端都得到良好支持,并且您的服务器很容易生成和客户端使用。 不确定它有助于您的带宽问题... Well, there's always JSON. It should be well-supported on both ends and is easy for your server to generate and client to consume. Not sure it helps with your bandwidth concerns any...
  • 如果你想开始新的线程来处理新的连接,我认为错误是由running标志引起的。 running是静态变量,它在线程之间共享。 每当连接丢失时,您将running设置为false,但该线程将阻塞,直到新连接,并且running将再次变为true。 旧的线程不会关闭。 你需要打破循环,但你需要小心,因为开始新线程和关闭线程是并发的,也许第一个关闭线程和新线程开始完成,这将关闭应用程序,因为在某个时候,有效的线程运行。 private static void run(){ while (running){ ...

相关文章

更多

最新问答

更多
  • 以编程方式创建视频?(Create videos programmatically?)
  • 为什么开机慢上面还显示;Inetrnet,Explorer
  • javascript数组,如何改变这个数组结构(javascript arrays, how to change this array structure)
  • 在ASP.NET Web API中使用多个Get方法进行路由(Routing with multiple Get methods in ASP.NET Web API)
  • 用于backbone.js验证的自定义验证器(Custom validator for backbone.js validation)
  • const char *与其他指针有什么不同?(Is const char * different from other pointers? [duplicate])
  • 无效的列索引,使用PreparedStatement更新(Invalid column index , update using PreparedStatement)
  • watchOS WCSession'已配对'和'watchAppAvailable'不可用(watchOS WCSession 'paired' and 'watchAppAvailable' are unavailable)
  • CalledFromWrongThreadException在Android上执行JUnit测试(CalledFromWrongThreadException exercising JUnit tests on Android)
  • 如何把文件保存到你的应用程序目录中?(How to put\save files into your application directory? (adobe air))
  • 美元符号在Java方法描述符中的含义?(Meanings of dollar sign in Java method descriptor?)
  • font-size的含义是什么:1em / 2em?(What doe the meaning of font-size:1em/2em?)
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • Android - 检测与特定wifi ssid断开连接的正确方法?(Android - Correct way to detect disconnecting from a particular wifi ssid?)
  • 通过Shell脚本将文件转换为另一个文件(Convert File To Another File By Shell Script)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • 如何过滤magento废弃的购物车报告集合(How to Filter the magento abandoned cart report collection)
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • web api http post传递对象504接收失败(web api http post passing object 504 Receive Failure)
  • Rails从视图编辑模型上的多个属性的方法(Rails way to edit multiple attributes on a model from a view)
  • 总是用{}初始化对象是否是好习惯?(Is it good habit to always initialize objects with {}?)
  • 在方案中编写特殊字符到输出端口(编译器设计)(writing special characters to output port in scheme (compiler design))
  • 电脑等级考试得证有多大用处?
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • 第一次调用函数将无法按预期工作,但下一次工作正常(calling a function on the first time won't work as expected, but next time is working)
  • 如何优化使用BigInteger操作执行时间的代码(How to optimize the code that uses BigInteger operations for execution time)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何提供个人资料信息,以便Passport.js可以使用它?(how does Profile information should be provided so Passport.js can use it?)
  • 有没有办法初始化jquery数据表中的细节?(is there any way to initialize details in jquery datatable?)