Disposing Form object on Mission Planner Exit()

Hello.

Thank you to the ArduPilot community. I’m currently studying Mission Planner plugins.

The form is not properly closing. As shown in the video above, if I add Form.Close() in the plugin’s Exit() method, the Mission Planner console window does not close.

Am I causing any problems or conflicts?

I would appreciate an expert’s opinion.

Below is my code for Exit() and Forms().

  1. Exit()
public override bool Exit()
{
       myClient?.Close(); 
       myStream?.Close(); 
       if (formAISRawData != null)
       {
            formAISRawData.Close(); 
            //formAISRawData.Dispose();
       }
      return true;
} 

2.Form Code

public class FormAISRawData : Form
{
    private Label lbl_RawData;
    private Label lbl_ProcessedData;
    private TextBox txb_RawData;
    private TextBox txb_ProcessedData;
    private Button button_StartStop;

    public FormAISRawData()
    {
        lbl_RawData = new Label();
        lbl_ProcessedData = new Label();
        txb_RawData = new TextBox();
        txb_ProcessedData = new TextBox();
        button_StartStop = new Button(); 

        Text = "FormAISRawData";
        Size = new Size(500, 150);
        FormBorderStyle = FormBorderStyle.FixedDialog;
        StartPosition = FormStartPosition.CenterScreen;

        lbl_RawData.Location = new Point(10, 20);
        lbl_ProcessedData.Location = new Point(10, 50);
        txb_RawData.Location = new Point(lbl_RawData.Right + 5, 20);
        txb_ProcessedData.Location = new Point(lbl_RawData.Right + 5, 50);

        button_StartStop.Location = new Point(lbl_ProcessedData.Left, 80);
        button_StartStop.Text = "Start/Stop";
        button_StartStop.ForeColor = Color.Gray;
        button_StartStop.BackColor = Color.White;
        button_StartStop.FlatAppearance.MouseOverBackColor = Color.LightBlue;
        button_StartStop.FlatStyle = FlatStyle.Flat;
        button_StartStop.UseVisualStyleBackColor = false;

        button_StartStop.Click += button_StartStop_Click;

        Controls.AddRange(new Control[] {lbl_RawData,lbl_ProcessedData,txb_RawData,txb_ProcessedData,button_StartStop});
    }//end of FormAISRawData() Constructor

    private void button_StartStop_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Button is pressed");
    }
}//end of FormAISRawData Class

Below code worked, but I don’t fully understand why.

So I’m asking to expert’s opinion.

I made one change: I added a flag to signal the end of an infinite loop thread when closing the Mission Planner.

Exit() code

  public override bool Exit()
  {
      is_program_closed = true;
      myClient?.Close(); 
      myStream?.Close();
      if(formAISRawData != null)
      {
          formAISRawData.Close();
      }

      return true;
  }

Thread Code

 private void TCPReceiving() 
 {
     while(true)
     {
         byte[] buffer = new byte[100];

         if(is_program_closed) 
         {
             break;
         }

         if(!is_receiving || myStream == null)
         {
             continue;
         }

Multithreading and concurrency in c# is not an easy topic.
You should check out this book.
https://www.amazon.com/Concurrency-Cookbook-Asynchronous-Multithreaded-Programming/dp/149205450X

you never show the thread creation code
but my guess is set the thread as background