0

I have a form Modal ShowDialog. Trying the close the Form created if one of two things happen 1 button on the form is pushed or 2 a value changes in a static variable.So far the button works I can use another button to check the status of the value and view the change? How can I have the 2nd part close the form?

Main_Menu.cs
  public void OverloadTripearly()
    {
        
        OverloadTripEarly overloadTripEarly = new OverloadTripEarly();
        overloadTripEarly.Owner = this;
        overloadTripEarly.ShowDialog();             
      //  overloadTripEarly.Refresh();
     //   OLTrip = overloadTripEarly; // assign object to enter
    }

OverloadTripEarly.cs code
using System;
using System.Collections.Generic;
using System.ComponentModel;
 using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormTitlepageNew
{
public partial class OverloadTripEarly : Form
{
    public OverloadTripEarly()
    {
        InitializeComponent();  
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        if (backgroundWorker1.IsBusy)
        {
            backgroundWorker1.CancelAsync();
        }
        else
        {
            this.Close(); // close if button is pressed
            this.Dispose();
        }
    }

    private void OverloadTripEarly_Load(object sender, EventArgs e)
    {

    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        do
        {
            
                Thread.Sleep(100);

                GlobalVariables.OverloadTest = NIDMMPXIeSlot5ConsoleApplication.SingleResistance
                 MeasurementApp();
                if (double.IsNaN(GlobalVariables.OverloadTest))
                {
                    GlobalVariables.OverloadFlag = true;
                    this.Close();
                    e.Cancel = true;

                }
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true;
                    this.Close();
                    return;
                }
            
        } while (double.IsNaN(GlobalVariables.OverloadTest));
    }

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {

    }

    private void backgroundWorker1_RunWorkerCompleted(object sender, 
RunWorkerCompletedEventArgs   e)
    {
        if (e.Cancelled)
        { this.Close();
            this.Dispose();
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        GlobalVariables.OverloadTest = NIDMMPXIeSlot5ConsoleApplication.SingleResistance
         MeasurementApp();
        if (double.IsNaN(GlobalVariables.OverloadTest))
        {
            textBox1.Text = "NaN".ToString();
        }
        else
        { textBox1.Text = GlobalVariables.OverloadTest.ToString(); }
    }
}
}

1 Answers1

0

i think your Problem is, that you change the 'Win Form' from another thread.

 private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Cancelled)
        { this.Close();
            this.Dispose();
        }
    }

Backgroundworker is calling from another thread. So you have to check if InvokeIsRequired and if so then invoke the this.Close()

check this link

ChrisG
  • 116
  • 6