bool StartTasks(string cron, string task, bool noLog, ref string errMsg) { errMsg = ""; try { var crond = CronScheduling.CronDaemon.Start <string>( value => { try { string pathTask = m_path + "\\" + task; string invisible = ""; #pragma warning disable CS0162 // Unreachable code detected if (ProjectInstaller.invisibleUserService) { FileHelper.StartProcess(pathTask); // Works in User service, invisible } else // LocalSystem service // Windows Home: OK // Windows Workstation: OK // Windows Server 2012: Random bug: // StartProcessAsCurrentUser: GetSessionUserToken failed // WTSQueryUserToken requires the SE_TCB_NAME privilege // Use AdjustTokenPrivileges to enable it: // (see bCreateProcess: StartImpersonation: AcquireLoadUserProfilePriveleges) // http://www.vbforums.com/showthread.php?616830-RESOLVED-VB-Net-CreateProcessAsUser-API { string errMsg1 = ""; if (!ProcessExtensions.StartProcessAsCurrentUser(pathTask, out errMsg1)) { // Solution: invisible run invisible = " (" + errMsg1 + ": invisible)"; ProcessUtilities.CreateUIProcessForServiceRunningAsLocalSystem( pathTask, in_strArguments: ""); } } #pragma warning restore CS0162 // Unreachable code detected if (!noLog) { LogMessage("Service Cron: " + value + invisible); } } catch (Exception ex) { string errMsg1 = "Service Cron: " + value + crLf; errMsg1 += " Error:" + ex.Message + ex.StackTrace; LogMessage(errMsg1); } }); crond.Add(task + ": " + cron, cron); return(true); } catch (Exception ex) { errMsg = ex.Message; return(false); } }