ShutdownBlockReasonCreate - Create multiple reasons to display during logoff/shutdown
<p>I am using ShutdownBlockReasonCreate in my C# application to warn the user if a logoff/shutdown is going to effect a currently running method in my application.</p>

<p>I have two methods that will warn the user:  An "Upload" method and a "Download" method.</p>

<p>If the user starts a long Upload, I set the ShutdownBlockReasonCreate with a message like "Upload is currently running".</p>

<p>The problem I am having is that if a user starts a simple download while the long upload is running, ShutdownBlockReasonCreate is also called again with a message like "Download is currently running".</p>

<p>This overwrites the original "Upload is currently running" message.  So when my download method is finished, and I call ShutdownBlockReasonDestroy, the user is all of a sudden now able to shutdown and logoff even though the upload is still running.</p>

<p>I want to be able to call ShutdownBlockReasonCreate with an optional parameter that I can call so I can send it multiple strings on why the program will not allow windows to shutdown or logoff.</p>

<p>So when the user goes to logoff, I want it to display:</p>

<p>"Upload is currently running"<br/>
"Download is currently running"</p>

<p>if both an upload and download is running.</p>

<p><br/>
Can someone please modify my code to show me how to do this?  If I can pass something else other than "this.Handle"; maybe I can accomplish this but I am unsure how to do this.<br/><br/></p>

<pre><code>        //http://blogs.msdn.com/b/oldnewthing/archive/2012/06/14/10319617.aspx
        //http://bartdesmet.net/blogs/bart/archive/2006/10/25/Windows-Vista-_2D00_-ShutdownBlockReasonCreate-in-C_2300_.aspx
        [DllImport("user32.dll")]
        public extern static bool ShutdownBlockReasonCreate(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string pwszReason);

        [DllImport("user32.dll")]
        public extern static bool ShutdownBlockReasonDestroy(IntPtr hWnd);

        private bool isBlocked = false;

        protected override void WndProc(ref Message aMessage)
        {
            const int WM_QUERYENDSESSION = 0x0011;
            const int WM_ENDSESSION = 0x0016;

            if (isBlocked &amp;&amp; (aMessage.Msg == WM_QUERYENDSESSION || aMessage.Msg == WM_ENDSESSION))
                return;

            base.WndProc(ref aMessage);
        }

        private void StopShutdown(string strMessage)
        {
            try
            {
                //strMessage == Message to display in shutdown/logoff box
                if (ShutdownBlockReasonCreate(this.Handle, strMessage))
                {
                    isBlocked = true;
                   Console.WriteLine("++ StopShutdown successful");
                }
                else
                    Console.WriteLine("++ StopShutdown failed");

            }
            catch (Exception ext)
            {
                MessageBox.Show("++ StopShutdown Error:    " + ext.Message + " " + ext.StackTrace);
            }
        }

        private void ResetShutdown()
        {
            try
            {

                if (ShutdownBlockReasonDestroy(this.Handle))
                {
                    isBlocked = false;
                    Console.WriteLine("++ ResetShutdown successful");
                }
                else
                    Console.WriteLine("++ ResetShutdown failed");

            }
            catch (Exception ext)
            {
                MessageBox.Show("++ ResetShutdown Error:    " + ext.Message + " " + ext.StackTrace);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StopShutdown("Upload in Progress");
            MessageBox.Show("Upload in Progress");
            ResetShutdown();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            StopShutdown("Download in Progress");
            MessageBox.Show("Download in Progress");
            ResetShutdown();
        }
</code></pre>
