C# app hangs randomly when called from Process.Start()

前端 未结 2 1145
野趣味
野趣味 2021-02-09 20:02

I have a Windows service set up to manage custom .Net tasks. Organization is:

-Windows Service monitors a schedule and starts a worker .exe as needed.

-Worker .

相关标签:
2条回答
  • 2021-02-09 20:42

    I can't add a comment on your post yet (I'll be destroyed by other stackoverflow user for doing that but...)

    I encounter a problem quite similar to yours. My problem was coming from the Session where the service is Started. During the migration, the service started in a "Session 0" in the new OS and not as a "system" program. It was issuing with the permission of our program to run through the service. Maybe you should check this point.

    Sorry if I tell it as an answer and not in comment

    0 讨论(0)
  • 2021-02-09 21:03

    Per last note, pulling the form bit out of it all together appears to have fixed the issue. At this point I'm assuming that server 2012 doesn't like something about a winform app not being displayed in certain situations; since it worked most of the time and nothing had changed in the form's code.

    Service:

    // Create a temp folder and copy the executable to it
    // (so the source exe isn't always in use and can be swapped out)
    
    Process p = new Process();
    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = Path.Combine(temp.Path, Path.GetFileName(ExecutablePath));
    psi.Arguments = CommandLineArgs.ConcatenateList(" ");
    psi.UseShellExecute = false;
    psi.RedirectStandardError = true;
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = true;
    psi.CreateNoWindow = true;
    psi.ErrorDialog = false;
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    p.StartInfo = psi;
    p.Start();
    
    // read standard output if necessary, kill and reschedule if appears to be frozen, etc.
    

    Worker:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new AppContext());
        }
    }
    
    class AppContext : ApplicationContext
    {
        public AppContext()
        {
            // Do Stuff
        }
    }
    
    0 讨论(0)
提交回复
热议问题