从串口频繁读取数据,关闭时卡死

public void Stop()
        {
            if (_serialPort != null)
            {
                if (_serialPort.IsOpen)
                {
                    _serialPort.DataReceived -= _serialPort_GpsReceived;
                    this._serialPort.DiscardInBuffer();
                    this._serialPort.DiscardOutBuffer();
                    int i = Environment.TickCount;
                    while (Environment.TickCount - i < 2000 && _isReceiving)
                        System.Windows.Forms.Application.DoEvents();
                    _serialPort.Close();
                }
                this._serialPort.Dispose();
                _serialPort = null;
            }
        }


_isReceiving:确保接收处理函数完成的标识

posted @ 2015-05-19 15:45  cnblogs1_1  阅读(1162)  评论(0编辑  收藏  举报