解决我的对象处置异常

发布于 2024-11-25 09:39:19 字数 1329 浏览 2 评论 0原文

您好,any1 可以告诉我在哪里尝试捕获此异常或解决它。当我关闭接收句柄时,如果我仍然收到一些数据,则会出现此错误。

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();

    }
}

Hello can any1 tell me where to try catch this exception or solve it. Whenver i close my receive handle, if i still recieved some data, it comes up with this error.

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();

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-12-02 09:39:19

我很确定发生的情况是您正在另一个线程上执行阻塞的 ReadLine() 调用(通过委托的 BeginInvoke),但随后调用 Close()SerialPort 上,而它仍在 ReadLine() 调用中。当数据进入现已关闭(并因此被处置)的端口时,将引发异常。

通常的解决方案是在所有未完成的读取完成之前不要关闭端口。您可能需要设置读取超时,以确保它在某个时刻返回。请参阅此处的示例更多信息。

I'm pretty sure what's happening is that you are executing the blocking ReadLine() call on another thread (via your delegate's BeginInvoke) but then calling Close() on the SerialPort while it is still in the ReadLine() call. When data comes in on the now closed (and thus disposed) port, the exception is thrown.

The usual solution is to not close the port until any outstanding reads have finished. You may need to set a timeout on the read in order to ensure it will return at some point. See the example here for more info.

甜警司 2024-12-02 09:39:19

锁定块可能会解决 ReadLine_Callback 内的问题。另请检查 IAsyncResult.IsCompleted 状态。

lock(lockerobject)
{
   // your handler logic.
}

lock block would probably solve your problem inside the ReadLine_Callback. Also check the IAsyncResult.IsCompleted status.

lock(lockerobject)
{
   // your handler logic.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文