I have written a program to receive data from the serial port. I am doing some calculations using those data and writing results to 4 richTestBoxes real time.I have two buttons to open and close the serial port. Some times when the close button is pressed, the form get stucked.This is the code in the close button.But this is not happening every time.

private void btnClosePort_Click(object sender, EventArgs e)
{
comport.Close(); // comport is the object created in the class serialport
}

I think the reason is that many fuinctions are running while the port is being closed. Is there any method to stop all the current processes in the program and then close the comport????? or any other solution????

Recommended Answers

All 2 Replies

It is possible that you have a read or write action in the UI thread that is hanging because you have closed the serial port.
I recommend that you set a flag before closing the port and pause for a short time to allow other threads to finish whatever they are doing.
Ideally you should be using some interlocking to prevent multiple threads accessing the comport device simultaneously.
I generally do this by wrapping my serial port in a seperate class and using an object lock.

thnaxx nick...!!! I'll try this.....!!! let u know if theres any problem further...!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.