当前位置: 首页>>代码示例>>C#>>正文


C# Process.OnExit方法代码示例

本文整理汇总了C#中System.Diagnostics.Process.OnExit方法的典型用法代码示例。如果您正苦于以下问题:C# Process.OnExit方法的具体用法?C# Process.OnExit怎么用?C# Process.OnExit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Diagnostics.Process的用法示例。


在下文中一共展示了Process.OnExit方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Start

        public void Start(string hostId, Action<int> onConnected)
        {
            lock (_processLock)
            {
                if (_stopped)
                {
                    return;
                }

                int port = GetFreePort();
                var psi = new ProcessStartInfo
                {
                    FileName = _paths.Dnx ?? _paths.Klr,
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    Arguments = string.Format(@"""{0}"" {1} {2} {3}",
                                              Path.Combine(_paths.RuntimePath.Value, "bin", "lib", "Microsoft.Framework.DesignTimeHost", "Microsoft.Framework.DesignTimeHost.dll"),
                                              port,
                                              Process.GetCurrentProcess().Id,
                                              hostId),
                };

#if DNX451
                psi.EnvironmentVariables["KRE_APPBASE"] = Directory.GetCurrentDirectory();
                psi.EnvironmentVariables["DNX_APPBASE"] = Directory.GetCurrentDirectory();
#else
                psi.Environment["KRE_APPBASE"] = Directory.GetCurrentDirectory();
                psi.Environment["DNX_APPBASE"] = Directory.GetCurrentDirectory();
#endif

                _logger.LogVerbose(psi.FileName + " " + psi.Arguments);

                _designTimeHostProcess = Process.Start(psi);

                // Wait a little bit for it to conncet before firing the callback
                using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    var t1 = DateTime.UtcNow;
                    var dthTimeout = TimeSpan.FromSeconds(4);
                    while (!socket.Connected && DateTime.UtcNow - t1 < dthTimeout)
                    {
                        Thread.Sleep(500);
                        try
                        {
                            socket.Connect(new IPEndPoint(IPAddress.Loopback, port));
                        }
                        catch (SocketException)
                        {
                            // this happens when the DTH isn't listening yet
                        }
                    }
                }

                if (_designTimeHostProcess.HasExited)
                {
                    // REVIEW: Should we quit here or retry?
                    _logger.LogError(string.Format("Failed to launch DesignTimeHost. Process exited with code {0}.", _designTimeHostProcess.ExitCode));
                    return;
                }

                _logger.LogInformation(string.Format("Running DesignTimeHost on port {0}, with PID {1}", port, _designTimeHostProcess.Id));

                _designTimeHostProcess.EnableRaisingEvents = true;
                _designTimeHostProcess.OnExit(() =>
                {
                    _logger.LogWarning("Design time host process ended");

                    Start(hostId, onConnected);
                });

                onConnected(port);
            }
        }
开发者ID:tugberkugurlu,项目名称:omnisharp-roslyn,代码行数:74,代码来源:DesignTimeHostManager.cs

示例2: Start


//.........这里部分代码省略.........

                if (DnxSdk.TryParseFullName(fullName, out flavor, out os, out arch, out version) &&
                    string.Equals(flavor, "coreclr", StringComparison.OrdinalIgnoreCase) &&
                    !string.Equals(os, "win", StringComparison.OrdinalIgnoreCase))
                {
                    // Work around since DTH is broken on CoreCLR on *nix and OSX
                    var runtimeName = DnxSdk.GetFullName(version, "mono", os, arch);
                    runtimePath = Path.Combine(Path.GetDirectoryName(runtimePath), runtimeName);

                    _logger.LogInformation("Using '{0}' for design time host.", runtimePath);
                }

                var dthPath = Path.Combine(runtimePath, "bin", "lib", "Microsoft.Dnx.DesignTimeHost", "Microsoft.Dnx.DesignTimeHost.dll");

                // TODO: This is for backcompat. Once the dust settles, and MS.Framework.DTH goes away, remove this.
                if (!File.Exists(dthPath))
                {
                    dthPath = Path.Combine(runtimePath, "bin", "lib", "Microsoft.Framework.DesignTimeHost", "Microsoft.Framework.DesignTimeHost.dll");
                }

                var dnx = DnxPaths.FirstPath(runtimePath, "dnx", "dnx.exe");
                var klr = DnxPaths.FirstPath(runtimePath, "klr", "klr.exe");

                var psi = new ProcessStartInfo
                {
                    FileName =  dnx ?? klr,
                    CreateNoWindow = true,
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    Arguments = string.Format(@"""{0}"" {1} {2} {3}",
                                              dthPath,
                                              port,
                                              Process.GetCurrentProcess().Id,
                                              hostId),
                };

#if DNX451
                psi.EnvironmentVariables["KRE_APPBASE"] = Directory.GetCurrentDirectory();
                psi.EnvironmentVariables["DNX_APPBASE"] = Directory.GetCurrentDirectory();
#else
                psi.Environment["KRE_APPBASE"] = Directory.GetCurrentDirectory();
                psi.Environment["DNX_APPBASE"] = Directory.GetCurrentDirectory();
#endif

                _logger.LogDebug(psi.FileName + " " + psi.Arguments);

                _designTimeHostProcess = new Process();
                _designTimeHostProcess.StartInfo = psi;
                _designTimeHostProcess.OutputDataReceived += (sender, args) => this._logger.LogInformation(args.Data ?? string.Empty);

                this._designTimeHostProcess.Start();
                this._designTimeHostProcess.BeginOutputReadLine();

                // Wait a little bit for it to conncet before firing the callback
                using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
                {
                    var t1 = DateTime.UtcNow;
                    var dthTimeout = TimeSpan.FromSeconds(30);
                    while (!socket.Connected && DateTime.UtcNow - t1 < dthTimeout)
                    {
                        Thread.Sleep(500);
                        try
                        {
                            socket.Connect(new IPEndPoint(IPAddress.Loopback, port));
                        }
                        catch (SocketException)
                        {
                            // this happens when the DTH isn't listening yet
                        }
                    }

                    if (!socket.Connected)
                    {
                        // reached timeout
                        _logger.LogError("Failed to launch DesignTimeHost in a timely fashion.");
                        return;
                    }
                }

                if (_designTimeHostProcess.HasExited)
                {
                    // REVIEW: Should we quit here or retry?
                    _logger.LogError(string.Format("Failed to launch DesignTimeHost. Process exited with code {0}.", _designTimeHostProcess.ExitCode));
                    return;
                }

                _logger.LogInformation(string.Format("Running DesignTimeHost on port {0}, with PID {1}", port, _designTimeHostProcess.Id));

                _designTimeHostProcess.EnableRaisingEvents = true;
                _designTimeHostProcess.OnExit(() =>
                {
                    _logger.LogWarning("Design time host process ended");

                    Start(hostId, onConnected);
                });

                onConnected(port);
            }
        }
开发者ID:azhoshkin,项目名称:omnisharp-roslyn,代码行数:101,代码来源:DesignTimeHostManager.cs


注:本文中的System.Diagnostics.Process.OnExit方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。