Link to home
Start Free TrialLog in
Avatar of g_johnson
g_johnsonFlag for United States of America

asked on

Problems running a .Net executable from Task Scheduler, program uses Process.Start

I have a Windows Forms Program, C#, that does a lot of preliminary work and then wants to launch another exe.  This works perfectly when run "normally".  However, when run from Task Scheduler, the called program does not appear to start up.  I guess my question is "what is the difference between double-clicking on the exe and starting it from Task Scheduler?"  In both cases, we are administrator with full privileges.  Because we are on a 64-bit machine we have tried compiling specifically as x64 to see if Task Scheduler liked that better, but it did not work.  Thanks.

        private bool PostOrders()
        {
            DateTime pDate = DateTime.Now.Date;

            try
            {
                string FJParam1;
                string FJParam2;
                string FJParam3;

                FJParam1 = "OE03S3  OEOEMENUYYYP                    ";

                FJParam2 = this._Machine.PadRight(20);

                FJParam3 = (this._DB + this._User.Trim()).PadRight(20);

                string ErrMsg = "";
                string rootDir = "";
                rootDir = this.RootFolder(ref ErrMsg);


                if (ErrMsg.Trim().Length != 0)
                {
                    throw new Exception("Finding Root Folder: " + ErrMsg);
                }
                Directory.SetCurrentDirectory(rootDir);


                Process post = new Process();
                ProcessStartInfo psi = new ProcessStartInfo();


                psi.UseShellExecute = false;
                psi.CreateNoWindow = false;
                psi.FileName = rootDir + @"\TargetProgram.EXE";
                psi.Arguments = FJParam1 + FJParam2 + FJParam3;
                psi.Domain = this._Domain;
                psi.UserName = this._UserName;
                SecureString pwd = new SecureString();
                pwd.AppendChar('P');
                pwd.AppendChar('a');
                pwd.AppendChar('s');
                pwd.AppendChar('s');
                pwd.AppendChar('W');
                pwd.AppendChar('o');
                pwd.AppendChar('r');
                pwd.AppendChar('d');


                psi.Password = pwd;

                using (post = Process.Start(psi))
                {
                    post.WaitForExit();
                }


                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("At PostOrders: " + ex.Message);
            }
        }
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

are you sure your folder is correctly set? try this:
 psi.FileName = io.path.combine( rootDir , "TargetProgram.EXE");

Open in new window

Avatar of g_johnson

ASKER

Hi Eric -- this won't compile for me.  What does "io" represent?  I have a reference to System.IO but that doesn't seem to be it.  I get "the name io doesn't exist in the current context".
then try system.io.path.combine
"what is the difference between double-clicking on the exe and starting it from Task Scheduler?"
The difference is that double-clicking on the process starts it as the user that double-clicks.  Starting from task scheduler starts it as the user set in the tasks security options or as the task scheduler service user.User generated imageIf you provide a user in the tasks security options, the provided user must have the required security to access resources required by and affect changes made by the process.  You must also realize that the task itself is not interactive with the desktop which means that the user will not be able to interact with any pop-ups that block the tasks completion.

-saige-
saige -- The executable itself has no message boxes.  TargetProgram.exe does have message boxes, but they are handled by code.  No user is expected to click on them, they are automatically clicked.  Could that still be the issue, though?

Would this part act any differently if called by a Windows Service instead of Task Scheduler?
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It appears that the pop-ups in the called program are the culprit.  Even though handled by the program, they do want to "pop up" first.  By process of elimination, this is the only reasonable answer we can come up with.