I tried code like below to access the serial port but when I tried to close my form it hung and the software stopped responding.