我已经用C#编写了一个客户端-服务器应用程序。该服务器应用程序是Windows服务,基本上用于从自定义设备读取数据。客户端应用程序是Windows窗体,UI仅显示服务器状态并向其发送命令(例如:用于连接到设备的GoOnline,GoOffline等)。
为了在应用程序之间交换数据,我使用了.NET远程处理;因此,我们在应用程序中都引用了一个库,当用户通过Windows窗体(客户端)发送命令时,它将触发一个事件,该事件将在服务器上引起注意。应用程序之间的数据交换正常工作,但我认为以这种方式编写很复杂。您对使用简单代码实现我的目标有何建议?我认为最好的方法(但我不知道是否可行)是在两个应用程序内创建对同一对象的引用,并直接读/写变量的状态。
基本上:
// Remote Object (Library)
bool goOnline;
public bool GoOnline
{
get { return goOnline; }
set { goOnline = value; }
}
// --------------------------------------
// Client side
if (... conditions ...)
RemoteObj.GoOnline = true;
// --------------------------------------
//Server side
System.Timers.Timer tmrScheduler = new System.Timers.Timer();
tmrScheduler.AutoReset = true;
tmrScheduler.Enabled = true;
tmrScheduler.Interval = 1000;
tmrScheduler.Elapsed += tmrScheduler_Elapsed;
private void tmrScheduler_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (RemoteObj.GoOnline)
{
// Start communication with device
// When device is ONLINE, RemoteObj.GoOnline = false and send to client connection OK
}
}
是否可以在不使用两个应用程序端的事件的情况下“直接”读取属性?还有其他想法或建议吗?