Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:20:43+00:00 2026-06-13T06:20:43+00:00

I have 2 forms, one is MainForm and second is DebugForm . The MainForm

  • 0

I have 2 forms, one is MainForm and second is DebugForm. The MainForm has a button that sets up and shows the DebugForm like this, And passes a reference to an already opened SerialPort:

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

    DebugForm = new DebugForm(Connection);

    DebugForm.Closed += delegate
    {
        WindowState = FormWindowState.Normal;
        DebugForm = null;
    };

    DebugForm.Show();
}

In the DebugForm, I append a method to handle the DataReceived event of the serialport connection (in DebugForm’s constructor):

public DebugForm(SerialPort connection)
{
    InitializeComponent();
    Connection = connection;
    Connection.DataReceived += Connection_DataReceived;
}

Then in the Connection_DataReceived method, I update a TextBox in the DebugForm, that is using Invoke to do the update:

private void Connection_DataReceived(object sender, SerialDataReceivedEventArgs e)
{           
    _buffer = Connection.ReadExisting();
    Invoke(new EventHandler(AddReceivedPacketToTextBox));
}

But I have a problem. As soon as I close the DebugForm, it throws an ObjectDisposedException on the Invoke(new EventHandler(AddReceivedPacketToTextBox)); Line.

How can I fix this? Any tips/helps are welcome!

UPDATE

I found out if I remove the event in a button event click , and close the form in that button click, everything is fine and my debugform gets closed without any exception…how odd!

private void button1_Click(object sender, EventArgs e)
{
    Connection.DataReceived -= Connection_DebugDataReceived;
    this.Close();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:20:44+00:00Added an answer on June 13, 2026 at 6:20 am

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

    Thanks to JohnWein from MSDN

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two forms, one with a radio button that users must select to
I have a form like this: <form action='' onsubmit='void(0)' class=mainform> <label class=form3><input type=checkbox> One
I have two forms. First one is to decide button numbers by using jslider.
I (currently) have two forms, one that needs to call the other. In other
It is a relevent question with this previous one. Bacasically I have two forms,
I have two forms(one main form,and one submain form).I'd like to do something like
I'm building an application where I'd like to have nested forms inside of one
I am making a c# windows app that has one MainForm and many User
I have a mainform that sends messages and a random number of receive forms
I have two forms on one page and want to have the input boxes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.