private static extern bool CreateProcess( string lpApplicationName, string lpCommandLine, ref SecurityAttributes lpProcessAttributes, ref SecurityAttributes lpThreadAttributes, bool bInheritHandles, ProcessCreationFlags dwCreationFlags, IntPtr lpEnvironment, string lpCurrentDirectory, [In] ref StartUpInfo lpStartupInfo, out ProcessInformation lpProcessInformation);
public async Task <CreateProcessData> CreateTaskAsync() { CreateProcessData createProcessData = new CreateProcessData(); if (!System.IO.File.Exists(PathToProcess)) { return(createProcessData); } StartUpInfo startUpInfo = StartUpInfo; ProcessInformation processInformation; SecurityAttributes threadAttributes = new SecurityAttributes(); SecurityAttributes processAttributes = new SecurityAttributes(); var processCreated = CreateProcess( PathToProcess, null, ref processAttributes, ref threadAttributes, false, ProcessCreationFlags.CREATE_DEFAULT_ERROR_MODE, IntPtr.Zero, null, ref startUpInfo, out processInformation); if (processCreated) { createProcessData.Process = Process.GetProcessById(processInformation.dwProcessId); createProcessData.ProcessInformation = processInformation; return(await Task.Run(() => { // 5 second timeout... for ( int cTick = Environment.TickCount, eTick = (cTick + 5_000); cTick < eTick || !createProcessData.WindowLoaded; cTick++) { createProcessData.Process.Refresh(); if (!string.IsNullOrWhiteSpace(createProcessData.Process.MainWindowTitle)) { createProcessData.WindowLoaded = true; } } return createProcessData; })); } return(createProcessData); }
public static async Task <bool> CreateTaskAsync(string pathToProcess) { if (!System.IO.File.Exists(pathToProcess)) { return(false); } StartUpInfo startUpInfo = new StartUpInfo() { dwFlags = 0x00000001, wShowWindow = 7 }; ProcessInformation processInformation; SecurityAttributes threadAttributes = new SecurityAttributes(); SecurityAttributes processAttributes = new SecurityAttributes(); var processCreated = CreateProcess(pathToProcess, null, ref processAttributes, ref threadAttributes, false, ProcessCreationFlags.CREATE_DEFAULT_ERROR_MODE, IntPtr.Zero, null, ref startUpInfo, out processInformation); if (processCreated) { Process process = Process.GetProcessById(processInformation.dwProcessId); return(await Task.Run(() => { // Sometimes, when creating a new process, the MainWindowHandle property is zero until the two while loops finish... // while (!process.WaitForInputIdle(100)) { } while (string.IsNullOrWhiteSpace(process.MainWindowTitle)) { } return true; })); } return(processCreated); }
public static bool Create(string pathToProcess, ProcessCreationFlags processCreationFlags, out Process process, out ProcessInformation processInformation) { process = null; processInformation = default; if (!System.IO.File.Exists(pathToProcess)) { return(false); } StartUpInfo startUpInfo = new StartUpInfo(); SecurityAttributes threadAttributes = new SecurityAttributes(); SecurityAttributes processAttributes = new SecurityAttributes(); var processCreated = CreateProcess(pathToProcess, null, ref processAttributes, ref threadAttributes, false, processCreationFlags, IntPtr.Zero, null, ref startUpInfo, out processInformation); if (processCreated) { process = Process.GetProcessById(processInformation.dwProcessId); } return(processCreated); }