如何在异步过程中从shell中检索完整的结果?

发布于 2024-09-01 21:21:33 字数 2874 浏览 3 评论 0原文

请参阅这些帖子:此处1 和 < a href="https://stackoverflow.com/questions/2864078/how-avoids-deadlock-condition">here2 最后我通过构建异步解决方案解决了我的问题,并且效果很好!但是我面临一个问题,现在我的代码是这样的:

class MyProcessStarter
    {
        private Process process;
        private StreamWriter myStreamWriter;
        private static StringBuilder shellOutput = null;
        public String GetShellOutput { get { return shellOutput.ToString(); }}

        public MyProcessStarter(){
            shellOutput = new StringBuilder("");
            process = new Process();            
            process.StartInfo.FileName = "sqlplus";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.OutputDataReceived += new DataReceivedEventHandler(ShellOutputHandler);

            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            //process.StartInfo.RedirectStandardError = true;
            process.Start();
            myStreamWriter = process.StandardInput;
            process.BeginOutputReadLine();
        }

        private static void ShellOutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
                shellOutput.Append(Environment.NewLine + outLine.Data);
        }

        public void closeConnection()
        {
            myStreamWriter.Close();
            process.WaitForExit();
            process.Close(); 
        }

        public void RunCommand(string arguments)
        {
            myStreamWriter.WriteLine(arguments);
            myStreamWriter.Flush();
            process.WaitForExit(100);
            Console.WriteLine(shellOutput);
            Console.WriteLine("============="+Environment.NewLine);
            process.WaitForExit(2000);
            Console.WriteLine(shellOutput);            
        }
    } 

我的输入是这样的:

 myProcesStarter.RunCommand("myusername/mypassword");
 Console.writeline(myProcesStarter.GetShellOutput);

但是看看我的输出:

SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
=============


SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
Enter user-name: 
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

正如你所看到的,运行函数的输出在不同时间是不一样的!那么现在你会帮我一个忙并帮助我,我如何才能等到所有输出以其他方式完成意味着我如何自定义我的流程以等到输出完成?因为我想写一个sqlcompiler所以我需要shell的精确输出。

请尽快帮助我。thanxxxxxxxxxxxx :X

refer to these post : here1 and here2 at last I solve my problem by build a asynchronous solution,and it work well!!! but there is a problem that i face with it,now my code is like this:

class MyProcessStarter
    {
        private Process process;
        private StreamWriter myStreamWriter;
        private static StringBuilder shellOutput = null;
        public String GetShellOutput { get { return shellOutput.ToString(); }}

        public MyProcessStarter(){
            shellOutput = new StringBuilder("");
            process = new Process();            
            process.StartInfo.FileName = "sqlplus";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.OutputDataReceived += new DataReceivedEventHandler(ShellOutputHandler);

            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            //process.StartInfo.RedirectStandardError = true;
            process.Start();
            myStreamWriter = process.StandardInput;
            process.BeginOutputReadLine();
        }

        private static void ShellOutputHandler(object sendingProcess,DataReceivedEventArgs outLine)
        {
            if (!String.IsNullOrEmpty(outLine.Data))
                shellOutput.Append(Environment.NewLine + outLine.Data);
        }

        public void closeConnection()
        {
            myStreamWriter.Close();
            process.WaitForExit();
            process.Close(); 
        }

        public void RunCommand(string arguments)
        {
            myStreamWriter.WriteLine(arguments);
            myStreamWriter.Flush();
            process.WaitForExit(100);
            Console.WriteLine(shellOutput);
            Console.WriteLine("============="+Environment.NewLine);
            process.WaitForExit(2000);
            Console.WriteLine(shellOutput);            
        }
    } 

and my input is like this:

 myProcesStarter.RunCommand("myusername/mypassword");
 Console.writeline(myProcesStarter.GetShellOutput);

but take a look at my out put:

SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
=============


SQL*Plus: Release 11.1.0.6.0 - Production on Thu May 20 11:57:38 2010
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
Enter user-name: 
Connected to:
Oracle Database 11g Enterprise Edition Release 11.1.0.6.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

as u see the output for run a function is not same in different time!So now would you do me a faver and help me that how I can wait until all the output done in other mean how I can customize my process to wait until output finishing ?? because I want to write a sqlcompiler so I need the exact output of shell.

plz help me soon.thanxxxxxxxxxxxx :X

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十雾 2024-09-08 21:21:33

您应该在 SQL*Plus 上使用 -S 参数。根据帮助,这个参数

设置静音模式以抑制
显示 SQL*Plus 横幅,
提示和命令回显。

理想情况下,这将使输出一致。

You should use the -S parameter on SQL*Plus. According to the help, this paramter

Sets silent mode which suppresses the
display of the SQL*Plus banner,
prompts, and echoing of commands.

Ideally, this would make the output consistent.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文