简体   繁体   English

Process.Start需要很长时间才能启动外部应用程序

[英]Process.Start takes long time to start outside application

I have a method that launches a second exe. 我有一个启动第二个exe的方法。 The issue I'm having is that if I'm in debug mode in Visual Studio and I put a breakpoint directly after the Process.Start call my second application launches immediately but if I have no break points in VS or run my main C# application outside of VS the launching of my second application via Process.Start can take up to two minutes. 我遇到的问题是,如果我处于Visual Studio的调试模式下,并且在Process.Start调用之后直接放置一个断点,则第二个应用程序将立即启动,但是如果VS中没有断点或运行我的主C#应用程序在VS之外,通过Process.Start启动我的第二个应用程序最多可能需要两分钟。 My method is below and where I put my breakpoint to see an immediate launch of the 2nd app is at line "if(null != _ProcessMine)". 我的方法在下面,我放置断点以查看第二个应用程序的立即启动的位置是“ if(null!= _ProcessMine)”行。 I put the launch of the second exe in a worker thread because when I close my main exe I want the second exe to close also. 我将第二个exe的启动放在工作线程中,因为当我关闭主exe时,我也想关闭第二个exe。

    public static void RunBtnProcessThread(string processName, String sArgs, Button btn)
    {
        // disable the button until we release the newly launched process
        btn.Enabled = false;

        BackgroundWorker worker = new BackgroundWorker();

        worker.DoWork += (doWorkSender, doWorkArgs) =>
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.CreateNoWindow = false;
            startInfo.UseShellExecute = false;
            startInfo.FileName = processName;
            startInfo.Arguments = sArgs;
            try
            {   
                using ( _ProcessMine = Process.Start(startInfo))
                {
                    if(null != _ProcessMine)
                        _ProcessMine.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                string _Funk = ReflectionHelper.GetMethodFullName(MethodBase.GetCurrentMethod());

                // error
                Debug.Assert(false, "Error: " + ex.Message);

                // Log error.
                TraceUtil.LogException(_Funk, ex);
            }

            System.Threading.Thread.Sleep(500);
        };

        worker.RunWorkerCompleted += (completedSender, completedArgs) =>
        {
            btn.Enabled = true;

            _ProcessMine)= null;
        };            

        worker.RunWorkerAsync();
    }

You don't actually need a separate thread for your scenario. 您实际上不需要针对您的方案的单独线程。 You can accomplish the same thing by subscribing to the Process.Exited() event: 您可以通过订阅Process.Exited()事件来完成同一件事:

    public static void RunBtnProcessThread(string processName, String sArgs, Button btn)
    {
        // disable the button until we release the newly launched process
        btn.Enabled = false;

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.CreateNoWindow = false;
        startInfo.UseShellExecute = false;
        startInfo.FileName = processName;
        startInfo.Arguments = sArgs;

        try
        {
            _ProcessMine = Process.Start(startInfo);
            _ProcessMine.EnableRaisingEvents = true;
            _ProcessMine.Exited += (sender, e) =>
            {
                btn.Invoke((MethodInvoker)delegate { 
                    btn.Enabled = true; 
                });
                _ProcessMine = null;
            };
        }
        catch (Exception ex)
        {
            string _Funk = ReflectionHelper.GetMethodFullName(MethodBase.GetCurrentMethod());

            // error
            Debug.Assert(false, "Error: " + ex.Message);

            // Log error.
            TraceUtil.LogException(_Funk, ex);
        }
    }

You could close it using something like: 您可以使用以下方法将其关闭:

    void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (_ProcessMine != null && !_ProcessMine.HasExited)
        {
            // Depending on the type of app:
            _ProcessMine.CloseMainWindow();
            // ... or ...
            _ProcessMine.Kill(); 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM