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

前端 未结 2 1148
野趣味
野趣味 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 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
    {
        /// 
        /// The main entry point for the application.
        /// 
        [STAThread]
        static void Main()
        {
            Application.Run(new AppContext());
        }
    }
    
    class AppContext : ApplicationContext
    {
        public AppContext()
        {
            // Do Stuff
        }
    }
    

提交回复
热议问题