locked
MessageReceived event of DatagramSocket

    Question

  • HI,

    I have an application which receive data from multiple socket, I have created a MessageReceived event handler and assigned the same to all the DatagramSockets created. Now I want to know what will happen if the application receive data on say socket2 while processing the data received in say socket1 ?

    Thanks,

    AKEV 



    • Edited by AKEV Monday, December 3, 2012 1:17 PM
    Monday, December 3, 2012 1:16 PM

Answers

  • Hi  AKEV

    There is a possibility of conflicting due to processing received data. It depends actually what are you doing inside the data process procedure. As far I know, when data arrives, MessageReceived event fired and the desired method runs in a separate worker thread (background thread). So if you are using something which are not thread safe, you may got exception.

    In that case, you should put data processing code inside the critical section for avoiding data corruption.

    // Your processing method may be invoked more than once before completing the previous one.
            EnterCriticalSection(&lock);

                  // Process received data here

            LeaveCriticalSection(&lock);

    • Marked as answer by AKEV Tuesday, December 4, 2012 5:32 AM
    Monday, December 3, 2012 3:08 PM

All replies

  • Hi  AKEV

    There is a possibility of conflicting due to processing received data. It depends actually what are you doing inside the data process procedure. As far I know, when data arrives, MessageReceived event fired and the desired method runs in a separate worker thread (background thread). So if you are using something which are not thread safe, you may got exception.

    In that case, you should put data processing code inside the critical section for avoiding data corruption.

    // Your processing method may be invoked more than once before completing the previous one.
            EnterCriticalSection(&lock);

                  // Process received data here

            LeaveCriticalSection(&lock);

    • Marked as answer by AKEV Tuesday, December 4, 2012 5:32 AM
    Monday, December 3, 2012 3:08 PM
  • You should also know that sockets are special as far as the async processing goes.  All of the async methods in the runtime will work the same way, and you need to protect your data structures appropriately.
    Tuesday, December 4, 2012 2:28 PM
  • You should also know that sockets are special as far as the async processing goes. 
    Can you elaborate on this? What special concerns should I be aware of when working with WinRT sockets?
    Tuesday, December 4, 2012 3:19 PM