S
styles00
Help. I've been bashing my head on this for a while.
I'm creating an application that is meant to control a hardware device
via a socket. It's main purpose is to display the current state of the
hardware (operating temperature, current operating state, etc...), and
also allow the user to change various settings on the hardware. The
application must poll the device periodically to check for any status
changes.
Below is a simplistic representation of the class I am using to
represent the device (Not my actual code, but it illustrates what I am
currently doing)
class RemoteDevice
{
private Socket sock;
private Byte[] recvBytes = new Byte[256];
public void Connect(string addr)
{
// Connect via socket
}
public void Disconnect()
{
// Disconnect socket
}
private string uptime;
public string Uptime
{
get { return uptime; }
}
private int currentOperatingLevel;
public int CurrentOperatingLevel
{
get { return CurrentOperatingLevel }
}
private bool enableDevice;
public bool EnableDevice
{
get { return enableDevice }
}
public void EnableDevice(bool enable)
{
string enableMsg = "Enable device " + enable.ToString();
Byte[] msg = ASCII.GetBytes(enableMsg);
sock.Send(msg);
}
public void SetOperatingLevelDevice(int level)
{
string levelMsg = "Set Operating Level " + level.ToString();
Byte[] msg = ASCII.GetBytes(levelMsg);
sock.Send(msg);
}
public int Poll()
{
string pollMsg = "Get Current Stats"
Byte[] msg = ASCII.GetBytes(pollMsg);
sock.Send(msg);
Int32 bytes = sock.Receive(recvBytes, recvBytes.Length, 0);
string recvMsg = ASCII.GetString(recvBytes, 0, bytes);
string items[] = recvMsg.Split(" ".ToCharArray());
uptime = items[0];
enableDevice = bool.Parse(items[1]);
currentOperatingLevel = int.Parse(items[2]);
}
}
I've created a thread queue to queue the messages being sent to the
device as the device itself can only handle one operation at time.
Polling is implemented with the use of a timer. When the timer expires
I queue the 'Poll()' operation. The 'SetOperatingLevelDevice(int
level)' and 'EnableDevice(bool enable)' operations ' also run in a
thread to prevent the UI from freezing.
The UI displays all the properties of the connected device, and
provides some controls to configure the connected device.
Because the UI is using the "RemoteDevice" class to also display the
representation of the device to the user, I would have to provide
thread synchronization on all properties within the class. In my real
class I have at least 2 dozen properties which means that would be a
lot of lock() 's to provide.
If I wanted to use this class in another application which didn't have
a UI (and didn't need to be threaded) then that's a lot of locking/
unlocking for no reason. Also, I have an issue if I want to implement
databinding, as any updates would not take effect on the UI thread.
Is there a better way to implement this? I'd like to think that I'm
not the only one who had a problem similar to this. All the examples
of threading worker tasks had nice clean ways of separating the work
from the model. Mind you the examples were pretty simplistic too =).
Thanks,
- Mike
I'm creating an application that is meant to control a hardware device
via a socket. It's main purpose is to display the current state of the
hardware (operating temperature, current operating state, etc...), and
also allow the user to change various settings on the hardware. The
application must poll the device periodically to check for any status
changes.
Below is a simplistic representation of the class I am using to
represent the device (Not my actual code, but it illustrates what I am
currently doing)
class RemoteDevice
{
private Socket sock;
private Byte[] recvBytes = new Byte[256];
public void Connect(string addr)
{
// Connect via socket
}
public void Disconnect()
{
// Disconnect socket
}
private string uptime;
public string Uptime
{
get { return uptime; }
}
private int currentOperatingLevel;
public int CurrentOperatingLevel
{
get { return CurrentOperatingLevel }
}
private bool enableDevice;
public bool EnableDevice
{
get { return enableDevice }
}
public void EnableDevice(bool enable)
{
string enableMsg = "Enable device " + enable.ToString();
Byte[] msg = ASCII.GetBytes(enableMsg);
sock.Send(msg);
}
public void SetOperatingLevelDevice(int level)
{
string levelMsg = "Set Operating Level " + level.ToString();
Byte[] msg = ASCII.GetBytes(levelMsg);
sock.Send(msg);
}
public int Poll()
{
string pollMsg = "Get Current Stats"
Byte[] msg = ASCII.GetBytes(pollMsg);
sock.Send(msg);
Int32 bytes = sock.Receive(recvBytes, recvBytes.Length, 0);
string recvMsg = ASCII.GetString(recvBytes, 0, bytes);
string items[] = recvMsg.Split(" ".ToCharArray());
uptime = items[0];
enableDevice = bool.Parse(items[1]);
currentOperatingLevel = int.Parse(items[2]);
}
}
I've created a thread queue to queue the messages being sent to the
device as the device itself can only handle one operation at time.
Polling is implemented with the use of a timer. When the timer expires
I queue the 'Poll()' operation. The 'SetOperatingLevelDevice(int
level)' and 'EnableDevice(bool enable)' operations ' also run in a
thread to prevent the UI from freezing.
The UI displays all the properties of the connected device, and
provides some controls to configure the connected device.
Because the UI is using the "RemoteDevice" class to also display the
representation of the device to the user, I would have to provide
thread synchronization on all properties within the class. In my real
class I have at least 2 dozen properties which means that would be a
lot of lock() 's to provide.
If I wanted to use this class in another application which didn't have
a UI (and didn't need to be threaded) then that's a lot of locking/
unlocking for no reason. Also, I have an issue if I want to implement
databinding, as any updates would not take effect on the UI thread.
Is there a better way to implement this? I'd like to think that I'm
not the only one who had a problem similar to this. All the examples
of threading worker tasks had nice clean ways of separating the work
from the model. Mind you the examples were pretty simplistic too =).
Thanks,
- Mike