none
Serial Port and Threads RRS feed

  • Question

  • I am writing a program that sends and receives data over serial ports using the SerialPort component. Serial port data arrives synchronously, so I am processing that data in a DataReceived handler.

    I understand that the DataReceived handler runs in a different thread than the main GUI and that I can not directly update the GUI from the DataReceived hander. Instead, I user BeginInvoke to invoke a delegate on the main GUI thread.

    This approach seems to work. I have run my program for over an hour with data coming in and I am not getting any thread errors and memory usage is constant, so I'm guessing there are no memory leaks. 

    But whenever I terminate the program, the Output window displays several messages like:

    "The thread 0xXXXX has exited with code 0 (0x0)."

    before the "The program … has existed with code 0 (0x0)." message

    It seems that the longer I run my program, the more of the "The thread …" messages there are.

    If I comment out the BeginInvoke calls, the "The thread …" message go away, so I assume they are related to BeginInvoke.

    Am I passing data from the DataReceived handler to the GUI thread properly? Should I be concerned about all the threads.

    I added some code to monitor the available worker and IC threads and there doesn't appear to be any leakage. I set the number of threads to 8 / 8 and when I check it usually has 7 or 8 of each available.

    Thanks,
    Mark

    Sunday, January 27, 2019 3:57 AM

Answers