Background program windows

MiPakTeh 1,476 Reputation points
2021-09-08T04:31:28.107+00:00

I create code to shutdown computer.I don't wont the warning after procced event Button_1.
"The program is still running in background" just shutdown this computer.

thank.

Code Form;

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

namespace Abot_5
{
    public partial class Form1 : Form
    {
        private StrtUp_ MyNameClass;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MyNameClass = new StrtUp_(this);
            MyNameClass.DoExitWin(StrtUp_.EWX_REBOOT);
        }
    }
}

code class;

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

namespace Abot_5
{
    class StrtUp_
    {
        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        internal struct TokPriv1Luid
        {
            public int Count;
            public long Luid;
            public int Attr;
        }

        [DllImport("kernel32.dll", ExactSpelling = true)]
        internal static extern IntPtr GetCurrentProcess();

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool OpenProcessToken(IntPtr h, int acc, ref IntPtr
        phtok);

        [DllImport("advapi32.dll", SetLastError = true)]
        internal static extern bool LookupPrivilegeValue(string host, string name,
        ref long pluid);

        [DllImport("advapi32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool AdjustTokenPrivileges(IntPtr htok, bool disall,
        ref TokPriv1Luid newst, int len, IntPtr prev, IntPtr relen);

        [DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
        internal static extern bool ExitWindowsEx(int flg, int rea);

        internal const int SE_PRIVILEGE_ENABLED = 0x00000002;
        internal const int TOKEN_QUERY = 0x00000008;
        internal const int TOKEN_ADJUST_PRIVILEGES = 0x00000020;
        internal const string SE_SHUTDOWN_NAME = "SeShutdownPrivilege";
        internal const int EWX_LOGOFF = 0x00000000;
        internal const int EWX_SHUTDOWN = 0x00000001;
        internal const int EWX_REBOOT = 0x00000002;
        internal const int EWX_FORCE = 0x00000004;
        internal const int EWX_POWEROFF = 0x00000008;
        internal const int EWX_FORCEIFHUNG = 0x00000010;
        private Form1 form1;

        public StrtUp_(Form1 form1)
        {
            this.form1 = form1;
        }

        public void DoExitWin(int flg)
        {
            bool ok;
            TokPriv1Luid tp;
            IntPtr hproc = GetCurrentProcess();
            IntPtr htok = IntPtr.Zero;
            ok = OpenProcessToken(hproc, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, ref htok);
            tp.Count = 1;
            tp.Luid = 0;
            tp.Attr = SE_PRIVILEGE_ENABLED;
            ok = LookupPrivilegeValue(null, SE_SHUTDOWN_NAME, ref tp.Luid);
            ok = AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero);
            ok = ExitWindowsEx(flg, 0);
        }

    }
}

What should I add in this code?

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,606 Reputation points
    2021-09-08T07:33:40.257+00:00

    If you just want to use the code to shut down the computer, using Process to call shutdown.exe may be the easiest way.

                    Process process = new Process();  
                    process.StartInfo.FileName = "shutdown.exe";  
                    process.StartInfo.Arguments = @"-s -t 0";  
                    process.Start();  
    

    If this method is not suitable for you, or I misunderstood what you mean, please let me know.


    If the response is helpful, please click "Accept Answer" and upvote it.
    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 additional answers

Sort by: Most helpful
  1. Castorix31 90,681 Reputation points
    2021-09-08T07:55:39.717+00:00

    .I don't wont the warning after procced event Button_1. "The program is still running in background" just shutdown this computer.

    Add the EWX_FORCE flag

    0 comments No comments

  2. Artemiy Moroz 271 Reputation points
    2021-09-08T11:12:14.667+00:00

    hi there!
    You may want to add a EWX_FORCE flag to your shutdown code. But bear in mind this may cause all user's work in the Word processor or similar. to be lost. For your reference, I am sending you the code to Shutdown Windows which I was using for years, and it works.

    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.