是什么原因导致我的用户界面在关闭的串行端口时冻结? [英] What causes my UI to freeze when closing a serial port?

查看:129
本文介绍了是什么原因导致我的用户界面在关闭的串行端口时冻结?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个串行端口相关的应用程序。虽然使用的SerialPort 我需要更新与接收字节的文本框 DataReceived 事件:

I am working on a serial port related application. While using DataReceived event of SerialPort I need to update a textbox with the received bytes:

private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    var data = Connection.ReadExisting();
    _readBuffer.Add(indata);

    Invoke(new EventHandler(AddReceivedPacketToTextBox));       
}

所以,我用调用更新文本框。但是有一个很大的问题。当我尝试,我的UI被冻结,关闭连接,我觉得这是becase的调用正在做somehing可能。

So I use Invoke to update the textbox. But there is a big problem. When I try to close connection, my UI gets freezed, I think this is becase Invoke is doing somehing perhaps.

一个朋友说我应该用 RequiredInvoke ,但我不知道他真正的精神疾病。如何关闭没有搞乱的调用和UI线程的连接?

A friend said I should use RequiredInvoke, but I have no idea what he ment really. How can I close the connection without messing up the invoke and UI thread?

下面是我的亲密方式:

private void DisconnectFromSerialPort()
{
    if (Connection != null && Connection.IsOpen)
    {
        Connection.Close();            
    }
}

更新

正如汉斯说我改变了调用的BeginInvoke ,但现在它有点糟糕的是,我的应用程序停止由于工作 InvalidOperationException异常因为集合 _readBuffer 的修改(那是什么的详细说,在VS)

As Hans said I changed Invoke to BeginInvoke but now its a bit worse, my application stops working due to InvalidOperationException because the collection _readBuffer was modified (Thats what the detail says in VS)

下面是我的$ C $下添加文本到文本框:

Here is my code for adding text to textbox:

private void AddReceivedPacketToTextBox(object sender, EventArgs e)
{

    foreach (var i in _readBuffer)
        tbIn.Text += string.Format("{0:X2} ", i);

    tbIn.Text += Environment.NewLine;

    ScrollToBottom(tbIn);

    label4.Text = _receivedPackets.ToString();
    _receivedPackets++;

    _readBuffer.Clear(); //Possibly because clearing collection gets out of sync with BeginInvoke??         
}

2日更新

我仍然有这个问题,改变了的invoke()的BeginInvoke 没;吨的帮助。我也尝试添加断开连接,形成闭合事件NU成功......只要我闭上我的形式,它获得的股票(我的意思是它的父的形式,因为这种形式可以访问的SerialPort正在从另一个form`调用。

I still have the problem, changing the Invoke() to BeginInvoke didn;t help. I also tried to add disconnect to form closing event nu success...anytime I close my form it gets stock (I mean its parent form, because this form that has access to serialport is being called from another form`.

我的意思是我想通了,用户界面​​被锁定只有2例:如果我时钟一个按钮,要求的Connection.close()另外,如果我试图关闭形式,即某些对象被设置父窗体会抛出异常。

I mean I figured out that the UI gets locked only in 2 cases: If I clock a button which calls Connection.Close() also if I try to close the form, the parent form will throw exception that some objects are disposed.

我称这样的串行形式从父窗体:

I call the serial form like this from the parent form:

public DebugForm DebugForm;

private void button1_Click(object sender, EventArgs e)
{
    if (DebugForm != null)
    {
        DebugForm.BringToFront();
        return;
    }

    DebugForm = new DebugForm();
    DebugForm.StartPosition = FormStartPosition.CenterScreen;
    DebugForm.Closed += delegate
                             {
                                 WindowState = FormWindowState.Normal;
                                 DebugForm = null;
                             };

    DebugForm.Show();
    WindowState = FormWindowState.Minimized; 
}

难道这是问题吗?!

Could this be the problem?!

推荐答案

这个问题可以通过添加一个计时器来解决:

The problem could be solved by adding a timer:

  bool formClosing = false;
    private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
      if (formClosing) return;
      _buffer = Connection.ReadExisting();
      Invoke(new EventHandler(AddReceivedPacketToTextBox));
    }
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
      base.OnFormClosing(e);
      if (formClosing) return;
      e.Cancel = true;
      Timer tmr = new Timer();
      tmr.Tick += Tmr_Tick;
      tmr.Start();
      formClosing = true;
    }
    void Tmr_Tick(object sender, EventArgs e)
    {
      ((Timer)sender).Stop();
      this.Close();
    }

由于JohnWein从<一个href="http://social.msdn.microsoft.com/Forums/en/winforms/thread/cd2f052c-b245-4df7-a935-d0b1be943cf3"相对=nofollow> MSDN

这篇关于是什么原因导致我的用户界面在关闭的串行端口时冻结?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆