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
最满意答案
如果您专门处理发布/订阅方案,我建议使用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.
相关问答
更多-
C# - WCF客户端/服务器在服务应用程序上打开控制台(C# - WCF Client/Server Open a Console on Service Application)[2022-07-03]
如果您需要这个用于开发目的,我可以推荐单元测试。 在我使用单元测试之前,我会像你描述的那样开发:创建一个控制台服务器和客户端,并与控制台客户端交互,以查看发送和接收的数据是否有意义。 我现在用单元测试做的是,我创建的小测试与我通过客户端控制台手动完成的操作大致相同。 单元测试方法具有以下优点: 测试小而清晰,所以我知道我在测试什么; 测试是可重复的,所以我知道当所有测试通过时,我之前测试过的小片仍然有效; 测试很容易运行,就像控制台客户端一样简单; 当我创建了足够的测试时,我可以在我无法与控制台交互的应用程 ... -
您当前在具有相同TCP连接的同一端口上启动一组Listener实例。 这不会像你想象的那样(你已经注意到了)。 诀窍是有一个打开Socket并接受客户端。 当客户端被接受时,您会生成一个处理通信的新线程。 基本方案是: 创建Socket 调用BeginAcceptSocket 在BeginAcceptSocket回调中,为连接的客户端生成一个新线程 再次调用BeginAcceptSocket以侦听新客户端 You currently start a set of Listener instances on ...
-
如何在C#中的客户端 - 服务器应用程序中处理数据库并发?(how to handle db concurrency in client-server application in C#?)[2022-05-25]
关于并发性问题,您的应用程序应设计为尽可能缩短与数据库的连接。 数据库上的每个操作都应该包括:打开连接,对数据库执行操作,关闭连接而不是:打开连接,执行一系列可能与获取/更新/插入数据相关的工作,然后在“最后“关闭连接。 现在,关于应用程序并发性,最终会出现两种情况。 在方案一中,我将其称为“最后写入获胜”,无论最后写入给定行的连接是存储的数据的版本。 如果Alice然后Bob同时写入同一行的Name列,Bob的版本将是存储的。 这是迄今为止最简单的,但如果您可能有很多人更新相同的数据,则可能会出现问题。 ... -
使用flux架构模式与服务器 - 客户端应用程序(using flux architecture pattern with server-client application)[2022-12-14]
啊,更多的阅读帮助我搞清楚了。 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 ...
-
将客户端 - 服务器应用程序从C ++转换为C#[关闭](Converting client-server applicatoin from C++ to C# [closed])[2022-01-17]
我在搜索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){ ...