Hi Hossein,
I am facing the same problem,
my aplication too freezes while closing the port at the same point as yours.
Could you tell me how did you fix the problem?
Anticipating your reply.
Amarjeet.
QUOTE(HosseinSadeghi @ 13 Jul, 2007 - 11:47 PM)

Hello,
This is a code about serial port connection in POCKET PC. This code uses serial port Data_Recive Event. When data enter the input buffer it is read and shown in logTextBox.
Problem: When I want to close the port, the system freezes completely.
I am using .net compact framework 2.0 Sp2 and visual studio.net 2005 and
C# language.
Thanks,
Sadeghi
public partial class MainForm : Form
{
private Delegate UpdateHandler;
private void DoUpdate(Object s, EventArgs e)
{
logTextBox.Text += ReadLineBuffer;
}
private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
ReadLineBuffer = serialPort.ReadLine();
this.Invoke(UpdateHandler);
}
private void MainForm_Load(object sender, EventArgs e)
{
serialPort.Handshake = Handshake.None;
UpdateHandler = new EventHandler(DoUpdate);
}
private void connectMenuItem_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == false)
{
try
{
serialPort.Open();
this.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
}
catch(IOException Exeption)
{
MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand,MessageBoxDefaultButton.Button1);
}
}
private void disconnectMenuItem_Click(object sender, EventArgs e)
{
if (serialPort.IsOpen == true)
{
try
{
serialPort.DiscardInBuffer();
serialPort.DiscardOutBuffer();
this.serialPort.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort_DataReceived);
serialPort.Close(); //Error
}
catch (IOException Exeption)
{
MessageBox.Show(Exeption.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
}
}