c# - NI LabVIEW NetworkVariableManager 是否应该保持连接?
问题描述
我从一位同事那里收到了一些 C# 代码,用于与通过以太网连接的 cRIO 设备进行交互。我正在尝试提高代码质量,以使其对未来的用户更易于理解,但是我正在努力从 API 文档中提取一些相关信息。NetworkVariableManager
我的主要问题是留在该州是否会引起问题Connected
?
现在代码使用一个看起来像的类
public class RIOVar<T>
{
public readonly string location;
public RIOVar(string location)
{
this.location = location;
}
public T Get()
{
using(NetworkVariableReader<T> reader = new NetworkVariableReader<T>(location) )
{
reader.Connect();
return reader.ReadData().GetValue()
}
}
public void Write(T value)
{
using(NetworkVariableWriter<T> writer = new NetworkVariableWriter<T>(location) )
{
writer.Connect();
writer.WriteValue(value);
}
}
}
实际的类做的远不止这些,但实际与 cRIO 通信的部分基本上归结为这两个方法和location
数据成员。
我想知道的是,在构造函数中将它们作为类成员和reader
它们是否会更好(在构造它们时,连接应该是可能的),但我不知道这是否会对计算机和 RIO 相互通信的方式有一些不利影响(可能连接的管理器使用一些资源或程序必须维护某种寄存器......?)因此这里的管理器连接的方法仅用于读/写操作是更好的设计。writer
Connect
解决方案
保持变量连接将其支持资源保留在内存中:
- 线程
- 插座
- 数据缓冲区
在线帮助中列出了这些资源,但我不清楚该列表是否完整:
- NationalInstruments.NetworkVariable uses multiple threads to implement the reading and writing infrastructure. When reading or writing in a tight loop insert a Sleep call, passing 0, to allow a context switch to occur thereby giving the network variable threads time to execute.
- ... snip ...
- NationalInstruments.NetworkVariable shares resources such as sockets and data buffers among connections that refer to the same network variable in the same program.
In my opinion, I'd expect better runtime performance by connecting/disconnecting as infrequently as possible. For example, when the network is reachable, connect; when it isn't, disconnect.
推荐阅读
- python - Anaconda Spyder 无法启动
- microsoft-graph-api - 使用 Microsoft Graph beta getAllMessage api 时总是收到“未知错误”响应
- javascript - 为什么我只能取回最后一次“hasOwnProperty”调用的结果?
- javascript - 构建一个序列号数组
- python - 我想打印数组行但使用此代码我只打印最后一行
- c# - 在 UWP C# 中使用两个不同的字符串创建一个文本文件
- python - 使用熊猫将数据按id,var1分组为python中的连续日期
- c - 无法在 Windows 上的 Linux 子系统中创建消息队列
- docker - 如何为 nginx docker 设置 ssl
- python-3.x - 'Python manage.py runserver' "ValueError: Unable to configure handler 'mail_admins'".: