1

Hi I'm opening a form like this from my main form when the user makes a selection of a menu item.

private void commToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Command_Form Command_Form1 = new Command_Form();
            Command_Form1.ShowDialog();
           // Command_Form1.Dispose();    this didn't help
        }

Inside the form "Command_Form1" I close it like this when the user clicks on the close button

private void Close_button_Click(object sender, EventArgs e)
        {
          this.Close();    //I get the exception here 
        }

This process works fine once but on the second closing of the form (Which I hope is a completely different/new instance of the form) I get the error in the title of this post. This is the output in the debug window.

A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll 9there are three copies in the debug window if that means anything)

When I step into the Close() method that is where the error is thrown but I'm having difficulty understanding what is going on. Is there something that I can read to help me understand what is going on inside the close method?

Justin
  • 6,611
  • 3
  • 36
  • 57
user593082
  • 69
  • 2
  • 10
  • Your first version of this question at least mentioned you used a SerialPort. Pretty essential info to help us help you. – Hans Passant Feb 10 '11 at 16:49
  • Wait a minute, in a comment below you say "I still end up in the `internal bool CheckCloseDialog(bool closingOnly)` in the catch statment with an exception". Care to show us this code? How about the full stack trace of the exception, too? – Justin Feb 10 '11 at 17:07
  • 1
    Duplicate of http://stackoverflow.com/questions/4948087/i-can-only-close-a-form-once-invalidoperation-exception-invoke-or-begininvoke-c – Sogger Feb 10 '11 at 17:51

1 Answers1

1

From your comment:

Becuase the form talks (send/receives data) to a serial port and I need other forms to talk to the same serial port (I don't want two forms to be connected to the serial port at the same time) I think I need to get rid of the one form when I open another form so the hide thing doesn't really work for me. Maybe there is a way to hide and disconnect from the serial port???

Just a guess:

Are you subscribing to the DataReceived event of the serial port? If so, are you attempting to access controls or properties on Command_Form inside the DataReceived handler? This event can still be raised even when the form is disposed, causing this problem. Before closing the form, you may need to:

  • Close the serial port: port.Close(), or
  • Unregister from the DataReceived event: port.DataReceived -= handler

Alternatively, you could just Hide the form instead of closing it, if you don't need the port for anything else.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • Actually, I'm not sure if this is right. I don't know why the exception would be on the `this.Close()` line... – Justin Feb 10 '11 at 16:39
  • I do close the port I do this before I close the form – user593082 Feb 10 '11 at 17:04
  • private void Form1_FormClosing( object sender, System.Windows.Forms.FormClosingEventArgs e ) { Program.UserPort1.CloseComPort(Program.UserPort1.SelectedPort); SavePreferences(); } – user593082 Feb 10 '11 at 17:05
  • Apparently `UserPort1` is not a `System.IO.Ports.SerialPort`. What is it? – Justin Feb 10 '11 at 17:10
  • 1
    This seems to have fixed the problem private void Form1_FormClosing( object sender, System.Windows.Forms.FormClosingEventArgs e ) { ComPorts.UserInterfaceData -= new ComPorts.UserInterfaceDataEventHandler(AccessFormMarshal); Program.UserPort1.CloseComPort(Program.UserPort1.SelectedPort); SavePreferences(); } – user593082 Feb 10 '11 at 17:13
  • It is a class that I use to configure/control the serial port. I have delegates in that class that marshall the data from the serial port to my form and I have delagtes on the form that marshal the data from the form to the serial port – user593082 Feb 10 '11 at 17:21
  • because I assume that hey are on a different thread. I wish I knew what was creating threads in the application I hav threads running around everywhere and I can only guess where they started. – user593082 Feb 10 '11 at 17:23
  • Because I assume that they are on a different threads i.e. the serial port and the UI form. I wish I knew what was creating threads in the application. I have threads running around everywhere and I can only guess where they started, I certainly didn't fire up any in my code...that I can remember.......can anyone suggest something to read that explains how to work out the secret lives of threads in a .NET app. I found a lot of good stuff that explains what threads are why you use them etc but when I open my threads window in the debugger – user593082 Feb 10 '11 at 17:36
  • @user59 - SerialPort runs the DataReceived event on a separate thread, according to the [documentation](http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx). [Other threads you see](http://stackoverflow.com/questions/3476642) are the main thread, GC, finalizer, and debug threads. Do you have a lot more than that? – Justin Feb 11 '11 at 14:05
  • Hi Justin I have about six threads in my application so your description sounds right. I found the part in the serial port documentation where the DataReceived event has its own thread (I assumed this much). I reckon if I run outside the debugger the extra threads will be accounted for...except for the ones that you describe above so now things seem to be falling in place. Thanks much – user593082 Feb 14 '11 at 18:25