StringBuilder与ArgumentOutOfRangeException崩溃

时间:2017-10-04 00:28:42

标签: c#

我在事件查看器中发生了崩溃:

Exception Info: System.ArgumentOutOfRangeException
   at System.Text.StringBuilder.ToString()
   at XXX.BrainActions+<>c__DisplayClass11_0.<runApp>b__0(System.Object, 
System.EventArgs)

违规代码就是这个,为什么StringBuilder会导致这个异常呢?

       Process process = new Process();
        StringBuilder builder = new StringBuilder();

        try
        {
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            process.StartInfo.FileName = filename;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.WorkingDirectory = Themes.getInstance().getScriptPath();
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.EnableRaisingEvents = true;
            process.Exited += new EventHandler((object sender, System.EventArgs e) => {
                string consoleOutput = builder.ToString();
                if (!String.IsNullOrWhiteSpace(consoleOutput))
                {
                    Log.INFO("XXX", String.Format("Console output: {0}", builder.ToString()));
                }
                processesRunning.Remove(process);
                if (callbacks != null)
                {
                    callbacks.ActionCompleted(action);
                    if (processesRunning.Count == 0)
                    {
                        callbacks.AllActionsCompleted();
                    }
                }
            });
            process.Start();
            process.PriorityClass = ProcessPriorityClass.High;
            process.OutputDataReceived += (sender, args) => builder.Append(args.Data);
            process.ErrorDataReceived += (sender, args) => builder.Append(args.Data);
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();
            processesRunning.Add(process);
        }
        catch (Exception e)
        {
        }

StringBuilder用于基本获取外部进程的输出,并使用Log4net将其记录到控制台,Log4net基本上包含在Log()函数中。有任何想法吗?请注意,此次崩溃很少发生,我只看过一次,但有一次是解决问题的理由。

0 个答案:

没有答案