背景
我正在为一个c#应用程序编写一个node.js包装器。在这个包装器中,我继续通过Process.RedirectStandardOutput读取标准输出。该事件绑定到类onOutputDataReceived的实例中的函数ProcessManager。在同一实例中,还有一个自定义事件系统的实例。
ProcessManager
EventSystem eventSystem;
private void Start()
{
[...]
process.OutputDataReceived += onOutputDataReceived;
[...]
}
private void onOutputDataReceived(object sender, DataReceivedEventArgs e)
{
[...]
eventSystem.call(eventName, args);
}EventSystem
List<EventHandler> eventList;
public Boolean call(String eventName, dynamic args)
{
[...]
foreach (EventHandler handler in eventList)
{
handler(args);
}
[...]
}问题是在调用事件时发生的。下面是使用我的包装器的winforms应用程序的一个示例。
Wrapper.ProcessManager procMan;
procMan.eventSystem.on(eventName, (a) =>
{
button1.Text = someValue;
});运行时,应用程序与消息一起崩溃。
跨线程操作无效:从创建线程以外的线程访问的控件'button1‘
据我所知,我的问题是:
onOutputDataReceived是在它自己的线程中异步执行的。由于这个线程(只打算处理输出)继续调用事件,我无意中多线程了我的包装器,使实现它的任何人都更加困难。
基本上是,
一旦收到新的输出数据,我需要在维护eventSystem.call()实例其余部分的线程中运行行ProcessManager。对于如何才能做到这一点,有什么想法吗?
--我想到的解决方案是这样的
ProcessManager
Queue<string> waiting = new Queue<string();
EventSystem eventSystem;
private void onOutputDataReceived(object sender, DataReceivedEventArgs e)
{
[...]
waiting.Enqueue(eventName);
}
private void WhenReady()
{
while(waiting.Count > 0)
eventSystem.call(waiting.Dequeue());
}据我所见,这将涉及每隔x毫秒进行某种轮询,这并不像一个干净的解决方案。而且,在我看来,这样的解决方案对于没有收到消息的情况来说太昂贵了,对于某些消息来说太慢了。
发布于 2016-04-16 22:49:21
执行nodejs进程并读取其输出的代码不应该需要了解事件订阅者的线程需求。使订户满足自己的要求:
(a) =>
{
Invoke(new Action(() => button1.Text = someValue)); //marshal to UI thread
}您的暂定解决方案将无法工作,因为它将阻塞UI线程。
另外,waiting是以不同步的方式使用的.这是一个无关的错误。
https://stackoverflow.com/questions/36670456
复制相似问题