none
Reading a message queue as fast as possible from C# RRS feed

  • Question

  • Hi there,

    I'm currently developing on an application where CAN frames are being logged. The application is based on C#. The CAN driver on the other side creates a message queue where incoming data is being placed in.

    Here is a snippet of code how the queue is getting accessed from the C# app:

    ### Snippet

    [DllImport("coredll.dll", EntryPoint = "ReadMsgQueue")]
    private static extern bool CEReadMsgQueue(int hMsgQ, byte[] lpBuffer, int BuffSize, out int lpNumberOfBytesRead, int Timeout, out int dwFlags);

    int dwFlags = 0;
    int dwRead = 0;
    byte[] arData = new byte[16];
    int nMsgSiz = 16;
    bool bReadMsg = CEReadMsgQueue(m_hReceiveDataMSGQueue, arData, nMsgSize, out dwRead, INFINITE, out dwFlags);

    ### End Snippet

    This basically works. But, unfortunately, this is not quite efficient as far as performance is being concerned. When I do increase the data rate then write operation on the driver side is blocked too long. On driver (WriteMsgQueue) I also set the timeout to INFINITE.

    I also wrote a sample application in native C++ that does the same. Right there the performance issue does not occure.

    Do you have any hints for me? Are there any alternatives for accessing a message queue more efficient on C#? Any help would be appreciated.

    Thanks in advance and best regards,
    Peter

    btw my device is runnin on CE 6 R2 with .Net CF 3.5

    Thursday, August 11, 2011 7:58 AM

Answers

  • If you use a thread to read the message queue you maybe could try to increase the thread priority of reading the queue that might help improving the performance. Anyway C# will be always having a performance penalty.

    You also could use your C++ application to get the CAN data and than pass the data to your C# application in case you use the C# application only to show the data.

    Best regards,

    Mike

    • Marked as answer by pkuebler Tuesday, May 19, 2015 8:22 AM
    Thursday, August 11, 2011 10:00 AM

All replies

  • If you use a thread to read the message queue you maybe could try to increase the thread priority of reading the queue that might help improving the performance. Anyway C# will be always having a performance penalty.

    You also could use your C++ application to get the CAN data and than pass the data to your C# application in case you use the C# application only to show the data.

    Best regards,

    Mike

    • Marked as answer by pkuebler Tuesday, May 19, 2015 8:22 AM
    Thursday, August 11, 2011 10:00 AM
  • I agree with Mike.  There's nothing "wrong" with what you are doing.  Some packaging up of the parameters is going to occur when you make that call out of managed code.  As long as you are not doing the new byte[16] every time you make the call, I don't see anything that you can do to improve it other than change the priority.

    Paul T.

    Thursday, August 11, 2011 4:36 PM