I'm new to Stack Overflow so this is my first question on. I'm trying to make a console application that shuts down instantly in C#. Currently this is ALL the code that i have in the project.
class Program
{
[DllImport("advapi32.dll")]
static extern bool InitiateSystemShutdown(
[MarshalAs(UnmanagedType.LPStr)] string lpMachinename,
[MarshalAs(UnmanagedType.LPStr)] string lpMessage,
Int32 dwTimeout,
bool bForceAppsClosed,
bool bRebootAfterShutdown);
static void Main(string[] args)
{
InitiateSystemShutdown("localhost", null, 0, true, false);
}
}
For some reason it only works every once in a while. And by that I mean I could click it 20 times and it wont do anything. But on some occasions it works on the first try, and shuts down the computer as expected. But this needs to be perfect. So I was wondering if there was any professionals (I'm kinda of a beginner) out there who could give me some advice/code. Am I missing something? Is something out of place? Any help would be appreciated.