How do you prevent form closing when events or methods are still running?

Joseph Querijero 0 Reputation points
2024-10-23T21:26:27.7133333+00:00

I'm struggling with the issue of user prematurely closing (using the Red X button) a form while it is processing an event or another process is calling a method in it. Object references inside the running event or method might become invalid because the close process has been initiated. Is there a simpler approach than adding an isEventOrMethodRunning flag inside all my event handlers or methods, so I can check it and cancel the FormClosing event? It seems hack-ish. Basically, if there is a running form code, prevent the window from closing.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,917 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,008 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 46,456 Reputation points Microsoft Vendor
    2024-10-24T02:25:39.8566667+00:00

    Hi @Joseph Querijero , Welcome to Microsoft Q&A,

    You can use the FormClosing event in WinForms to achieve this goal. A more elegant way is to use the FormClosing event to detect whether the user attempts to close the window, and decide whether to allow closing based on the running status of the event or method.

    private bool isRunning = false; // Flag indicating whether an event or method is running
    
    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (isRunning)
    {
    MessageBox.Show("The operation is in progress and the form cannot be closed.");
    e.Cancel = true; // Cancel the closing operation
    }
    }
    
    // Example of a long-running operation
    private async void LongRunningOperation()
    {
    try
    {
    isRunning = true;
    // Simulate a long-running task
    await Task.Delay(5000);
    }
    finally
    {
    isRunning = false; // Reset the flag after the operation is completed
    }
    }
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Joseph Querijero 0 Reputation points
    2024-10-24T21:21:06.9933333+00:00

    Is there a more elegant way than to litter your code with isRunning flag?

    0 comments No comments

  3. Karen Payne MVP 35,436 Reputation points
    2024-10-25T16:28:36.6433333+00:00

    I would use logic in both a cancel button and in closing event were the work is asynchronous and has a cancellation token. This way, if the user cancels you can decide what to do. In the following example the question dialog is asynchronous which works with .NET Core 5 and higher.

    Also consider if you disable the red close button a user can still shutdown the application with Task Manager.

    form

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.