question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked TimonYang-MSFT commented

Background program windows

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?


dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

TimonYang-MSFT avatar image
0 Votes"
TimonYang-MSFT answered TimonYang-MSFT commented

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.

· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank..TimonYang;
I already use that code before but not fully shutdown when somebody leave computer without saving his file.
I try this code with timer, if time enough 8 hour then computer will shutdown everyday .

0 Votes 0 ·

@MiPakTeh-6272
How about adding -F to the command?
This will forcefully close the running application without warning the user, but at the same time, it may result in the loss of unsaved data.

0 Votes 0 ·

yup..computer shutdown with Unsaved all program but before that the message appear about 5 second then was gone.
TimonYang it possible to remove that message??.

0 Votes 0 ·
Show more comments
Castorix31 avatar image
0 Votes"
Castorix31 answered

.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


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ArtemiyMorozUA avatar image
0 Votes"
ArtemiyMorozUA answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.