NEWS

[C#] Bắt sự kiện Windows Shutdown hoặc Logoff, xuất cảnh báo cho người dùng muốn lưu dữ liệu lại không

[C#] Bắt sự kiện Windows Shutdown hoặc Logoff, xuất cảnh báo cho người dùng muốn lưu dữ liệu lại không
Đăng bởi: Thảo Meo - Lượt xem: 1806 08:35:26, 14/09/2022C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ các bạn cách bắt sự kiện windows shutdown hay logout để ứng dụng mình biết để hỏi người dùng có muốn lưu trữ dữ liệu hay thông tin lại không.

[C#] Prevent Windows Shutdown or Logout 

Dưới đây là hình ảnh, khi ứng dụng của bạn chưa tắt, mà bạn bấm nút Shutdown hoặc logout thì sẽ hiển thị thông tin như hình bên dưới:

show_message_windows_shutdown (1)

Khi máy tính windows Shutdown, Windows sẽ gởi 1 thông báo cho tất cả các ứng dụng đang chạy biết máy tính đang shutdown. 

WM_QUERYENDSESSION khi đang đóng phiên làm việc logout một tài khoản

WM_ENDSESSION khi đang shutdown máy tính

Phương thức: ShutdownBlockReasonCreate => show thông báo cho windows biết

ShutdownBlockReasonCreate(this.Handle, "Bạn có chắc chắn không cần lưu ứng dụng phải không?");

Thông tin message hiển thị như hình ảnh ở trên

Phương thức: ShutdownBlockReasonDestroy(this.Handle) => thông báo cho windows biết có thể shutdown hoặc logout.

Source code C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PreventShutDownWindows
{
    public partial class Form1 : Form
    {
        public const int WM_QUERYENDSESSION = 0x0011;
        public const int WM_ENDSESSION = 0x0016;
        public const uint SHUTDOWN_NORETRY = 0x00000001;

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool ShutdownBlockReasonCreate(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string reason);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool ShutdownBlockReasonDestroy(IntPtr hWnd);
        [DllImport("kernel32.dll")]
        static extern bool SetProcessShutdownParameters(uint dwLevel, uint dwFlags);
        public Form1()
        {
            InitializeComponent();
            // Define the priority of the application (0x3FF = The higher priority)
            SetProcessShutdownParameters(0x3FF, SHUTDOWN_NORETRY);
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION)
            {
                // Prevent windows shutdown
                ShutdownBlockReasonCreate(this.Handle, "Đừng quên save ứng dụng trước khi shutdown!");
                ThreadPool.QueueUserWorkItem(o =>
                {
                    // Simulate some work
                    Thread.Sleep(1000);
                    this.BeginInvoke((Action)(() =>
                    {
                        // This method must be called on the same thread as the one that have create the Handle, so use BeginInvoke
                        ShutdownBlockReasonCreate(this.Handle, "Bạn có chắc chắn không cần lưu ứng dụng phải không?");

                        // Allow Windows to shutdown
                      // ShutdownBlockReasonDestroy(this.Handle);
                    }));
                });

                return;
            }

            base.WndProc(ref m);
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Bắt sự kiện Windows Shutdown hoặc Logoff, xuất cảnh báo cho người dùng muốn lưu dữ liệu lại không
Đăng bởi: Thảo Meo - Lượt xem: 1806 08:35:26, 14/09/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.