Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:53:32+00:00 2026-06-11T03:53:32+00:00

I have a windows form application which is writing bunch of stuff in the

  • 0

I have a windows form application which is writing bunch of stuff in the database. Now some time ago I was asking for the solution of how to be sure that all requests (writing to the database) has been done before windows form is closed.

The reason why am I doing this is because it will be a self standing application which has an automatic shut down and therefore I need to be able to finish all my writing and reading from the database and be sure when I am closing form that all the job is done.

Before I tried this solution I had a problem of cutting a connection when writing in the database because of a form closing.

Now the problem is that when I have a shut down process my windows form application won’t close, so after some time it just cuts of the electricity which is not what I need.

Here is the code, and please tell me what is wrong with it? I need to close the form when program is sure that no task is working in the background.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
      // this.Visible = false; // optional
      // this.ShowInTaskbar = false; // optional
      Task db = Task.Factory.StartNew(() => DBUpdate());
      Task.WaitAll(db);
      if (this.shutdownRequested)
            Process.Start("shutdown.exe", "-s");
 }

 protected override void WndProc(ref Message ex)
 {
      if (ex.Msg == WM_QUERYENDSESSION)
      {
           Message MyMsg = new Message() { Msg = WM_CANCELMODE };
           base.WndProc(ref MyMsg);
           this.shutdownRequested = true;
      } else {
           base.WndProc(ref ex);
      }
 }

EDIT:

I just realized that this is working when I click the X button and form starts to close, but when pc goes into the shutdown phase then its not. Any idea? Maybe I need to change a event on something else and not on the FormClosing ? One more thing. I have saved a priority of the application process as a real-time process.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:53:34+00:00Added an answer on June 11, 2026 at 3:53 am

    If your process takes more than a couple of seconds, the OS will kill it during a shutdown. Your only hope is to abort the shutdown, do the work and call it again.

    Your process only takes a couple of seconds, and is dependent on the instance of the Form, so this solution does not create a new thread to do the work in. So you should hide the form to prevent use interaction, as the main thread will be locked up while this process is run.

    If a shutdown event tries to close the application. The application will abort the shutdown. It will need administrator privileges to be able to do this though.

    using System;
    using System.Diagnostics;
    using System.Threading;
    using System.Windows.Forms;
    using Timer = System.Windows.Forms.Timer;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private bool _isClosing;
            private bool _isRunningUpdate;
            private bool _isShutdown;
            private Timer timer;
    
            public Form1()
            {
                InitializeComponent();
                FormClosing += Form1_FormClosing;
                timer = new Timer();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (e.CloseReason == CloseReason.WindowsShutDown)
                {
                    // Abort Shutdown
                    Process.Start("shutdown.exe", "-a");
                    _isShutdown = true;
                }
    
                if (!_isRunningUpdate && _isClosing) return;
    
                // Set isClosing to true
                _isClosing = true;
    
                if (!_isRunningUpdate)
                {
                    _isRunningUpdate = true;
                    timer.Tick += DbUpdate;
                    timer.Interval = 500;
                    timer.Enabled = true; 
                    timer.Start();
                }
    
                // Cancel current close request
                e.Cancel = true;
    
                // Optional Hide() --- could display message
                // Hide();
            }
    
            private void DbUpdate(object sender, EventArgs e)
            {
                timer.Stop();
                Thread.Sleep(3000);
    
                _isRunningUpdate = false;
                if (_isShutdown)
                    Process.Start("shutdown.exe", "-s -t 10");
                if (_isClosing)
                    Close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a windows form application which interacts with a remote database. the database
I have a windows form application which consists of a bunch of controls, but
Im using c# .net , windows form application. I have a XML file which
I have a C# windows application which does the following: 1) the main form
I have a Windows Form Application in which clicking certain buttons create objects from
In a Windows Form Application, I have a ComboBox1 which gets initialized in InitializeComponent()
I have a windows form application which needs to be the TopMost. I've set
I am writing a csharp windows form application which attempts to check for the
I have a windows form application. I have a query which return me datetime
I have a windows form application in which the Form1 object's initial WindowState ==

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.