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

荒凉一梦 提交于 2019-12-03 16:51:33

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

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
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!