Hi guys,
I am having some issues trying to get the CommandReceived event fired to me.
I have tried the VB sample delivered with the installer, and it works fine.
However, when i try to get it to work in C# - I can’t.
My code looks something like this:
class K8090Wrapper
{
const string boardPort = "COM6";
private K8090Board board;
CommandReceivedEvent evt;
public K8090Wrapper()
{
try
{
board = new K8090Board(boardPort);
bool connected = board.Connect();
board.CommandReceived += new CommandReceivedEvent(board_CommandReceived);
Console.WriteLine("Connected: " + connected);
}
catch (Exception e)
{
Console.WriteLine("Exception caught in contructor", e);
}
}
void board_CommandReceived(object o, CommandEventArgs args)
{
Console.WriteLine("COMMAND RECEIVED!");
}
I can get the board to flash the LEDs without any problems. But when i push the buttons, or change state on the LEDs, i get no events fired.
Anyone got an idea about this? Or tried to program the board with C#?
Thanks in advance,
Martin
Your code is fine. BUT, i suspect you created a console application, so here’s what will happen for you: your application starts, you connect to the board, you set the event handler, your application terminates, you press a button and events get fired.
So, indeed, your events come in when your application is already terminated. This is because the K8090 uses ‘events’ or in a more difficult term asynchronous IO. This means that an application will need to ‘wait-and-do-nothing’ while events come in, a console application cannot do this easily.
Creating a windows forms application is the easiest solution
My code was fine indeed.
It seems that in the native code (K8090Native), when registering a listener, a windowHandle is needed.
And as a console application does not have a windowshandle, therefore there are no events.
Conclusion: To get events, a Form must exist and have the instance of the K8090. This must be considered as a big mistake by the developers of the API.
This still leaves me dangling - as my application shall be a service, and a service can not use a Form to show anything.
If anyone have a solution to this, please let me know.
Regards,
Martin
This was not a mistake, this is a common event-driven design to shield users from multithreading issues. You don’t actually need to create a form, use the MessageWindow class provided in the K8090 library (+ add the System.Windows.Forms assembly to your project) to create a message-only window which can process the messages from the K8090 DLL.
IntPtr hDevice = K8090Native.OpenDevice(PortName); // connect
// check if connection is OK
MessageWindow MyWindow = new MessageWindow(); // create a message-only window
K8090Native.RegisterListener(hDevice, MyWindow.Handle); // register the window handle
Then use the MessageReceived event of the MessageWindow class to capture messages coming from the K8090.
This is actually how the K8090Board component works so you could take a peek there too, or even better, use the K8090Board class (some minor modifications should get it to work in your service).