I have been getting this error while trying to execute a command prompt in C#. I have an app that is supposed to shut down a PC when a user is logged off but I need it to cancel the shutdown when the user logs back on again.
Example:
1. User logs off at 8:30, so I have code in my app to detect this user log off and I use ProcessStartInfo to execute ProcessStartInfo and give it a command to shutdown the PC at a given time (this time is taken from a database and it changes periodically (ie "psshutdown.exe -v 0 -s -t 08:37 "). 2. However if the user logs back on before 08:37 I don't want the shutdown to be executed and neither do I want the user to have to cancel it themselves. 3. To get around this I put in another ProcessStartInfo that aborts the shutdown when a user logs on (psshutdown.exe /a)
However when the code executes the psshutdown.exe when a user logs off, it gives me the following error:
The appplication was unable to start correctly (0xC0000142). Click OK to close the application
It is unable to start psshutdown.exe.
The code that is giving the error is as follows:
//code below takes the time from the database and calcualts the minutes from the current time TimeSpan mins = TaskDateTime.TimeOfDay.Subtract(DateTime.Now.TimeOfDay); //this string is to tell the psshutdown.exe to shutdown the PC after this number of minutes string argu = " -v 0 -s -t " + mins.TotalSeconds.ToString(); //code below gets the current directory because that is where psshutdown.exe is installed string dir = AppDomain.CurrentDomain.BaseDirectory.ToString(); //adds "psshutdown.exe" to the directory dir = dir + "psshutdown.exe"; //processStartInfo is passed the psshutdown exe directory and the argument ProcessStartInfo startinf = new ProcessStartInfo(dir, argu); startinf.Verb = "runas"; startinf.RedirectStandardOutput = true; startinf.CreateNoWindow = true; startinf.UseShellExecute = false; p.StartInfo = startinf; p.Start(); try { p.WaitForExit(10000); } catch (Exception u) { System.Windows.Forms.MessageBox.Show(u.ToString()); }
I think it is maybe because psshutdown does not have the time to execute. I have tried a timer delay but it didn't work.