none
Check If Bool Variable = True Before Allowing Form Close RRS feed

  • Question

  • This is C# winforms.  

    I am running a behind the scenes event that is completely hidden to the user, and when it completes I set bool allDone = true;

    I am needing a way that when the user clicks the X to close the forecheck if allDone = true;  if it does not, then display a message to the user that "Still processing", and have the form auto-check this value every 30 seconds on it's own w/o the user having to click the X again.

    How would I Do this?

    Tuesday, October 8, 2019 2:50 PM

Answers

All replies

  • Subscribe to FormClosing in the new constructor of the form.

    FormClosing += Form1_FormClosing;

    Use this logic (you might also show a message box after e.Cancel = true so they know the app has not crashed.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!allDone)
        {
            e.Cancel = true;
        }
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, October 8, 2019 3:02 PM
    Moderator
  • Hi,

    There is an event called OnClosing that has event arguments containing the Cancel property. Setting this property will prevent the form to actually close. You could start a timer, ticking every 30 seconds. And for every timer tick, check the allDone variable and close the dialog manually.

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (!_allDone)
        {
            _manuallyClosed = true;
            timer1.Enabled = true;
            e.Cancel = true;
        }
    }
    
    private void timer1_Tick(object sender, EventArgs e)
    {
        if (_allDone && _manuallyClosed)
        {
            Close();
        }
    }

    Kind regards,

    Johnny Hooyberghs

    Tuesday, October 8, 2019 3:03 PM
  • @Karen - how do I set a timer to re-check after X amount of time?

    @Hooyberghs - 

    how do I set the interval for the timer?  


    Tuesday, October 8, 2019 3:11 PM
  • How can I pass the value of allDone from HiddenProc back to Form1 so that I know when it's all clear?  See the sample code below to see what I'm working with...

    public partial class Form1 : Form
    {
    	private bool allDone = false;
    	
        public Form1()
    	{
    		InitializeComponent();
    		FormClosing += Form1_FormClosing;
    	}
    	
    	private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    	{
    		if (!allDone)
    		{
    			e.Cancel = true;
    		}
    	}
    		
    	private async void btnTest_Click(object sender, EventArgs e)
    	{
    		new Thread(() =>
    		{
    			HiddenProc ts = new HiddenProc(allDone);
    			ts.StartPosition = FormStartPosition.CenterParent;
    			ts.Show();
    		}).Start();
    	}
    }
    public partial class HiddenProc : Form
    {
    	private bool _allDone;
    
    	public HiddenProc(bool allDone)
    	{
    		_allDone = allDone;
    		InitializeComponent();
    	}
    	private void HiddenProc_Load(object sender, EventArgs e)
    	{           
    		GetData();
    	}
    	private void GetData()
    	{
    		//Do Stuff
    		
    		alldone = true;
    	}
    }

    Tuesday, October 8, 2019 3:38 PM
  • _allDone needs to be public property then look at the following answer where a label is used, you point to the public property.

    https://stackoverflow.com/questions/41014489/c-sharp-setting-getting-properties-of-controls-to-from-another-thread

    The thread need to be private or public form variable.

    https://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-make-thread-safe-calls-to-windows-forms-controls


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by IndigoMontoya Tuesday, October 8, 2019 6:54 PM
    Tuesday, October 8, 2019 4:15 PM
    Moderator