你好可以任何1告訴我在哪裏嘗試趕上這個異常或解決它。當我關閉我的接收句柄時,如果我仍然收到一些數據,它會出現此錯誤。解決我的對象處理異常
public partial class Form1 : Form
{
SerialPort sp;
IAsyncResult recv_result;
string buffer;
private delegate string ReadLine_Delegate();
private void button1_Click(object sender, EventArgs e)
{
try
{
sp = new SerialPort("COM8", 9600);
sp.Open();
sp.ReadTimeout = 50000;
sp.NewLine = "\n\r\0";
ReadLine_Delegate x = new ReadLine_Delegate(sp.ReadLine);
recv_result = x.BeginInvoke(new AsyncCallback(ReadLine_Callback),
x);
}
catch (Exception ex)
{
}
}
private void ReadLine_Callback(IAsyncResult iar)
{
ReadLine_Delegate y = (ReadLine_Delegate)iar.AsyncState;
try
{
buffer = y.EndInvoke(iar);
}
catch
{
MessageBox.Show("Error");
return;
}
ListBoxAdd(buffer);
buffer = "";
recv_result = y.BeginInvoke(new AsyncCallback(ReadLine_Callback), y);
}
private void disconnectButton_Click(object sender, EventArgs e)
{
recv_result.AsyncWaitHandle.Close();
sp.Close();
}
}
我很確定我幾個小時前見過這個問題。 –
看看異常的堆棧,看看它是什麼行。 –
[Stop Stream.BeginRead()]的可能重複(http://stackoverflow.com/questions/6759938/stop-stream-beginread) –