我如何调用C#应用程序Perl脚本? [英] How do I call Perl script in C# application?

查看:158
本文介绍了我如何调用C#应用程序Perl脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉一个Perl程序的输出,并在C#Windows窗体的文本框中显示输出数据(在屏幕上的字符串)。

I want to capture output of a Perl program and display output data (string on screen) in a text box on C# Windows Form.

下面是我的主要的C#代码:

Here is my main C# code:

public partial class frmMain : Form
{
    private Process myProcess = null;
    public frmMain()
    {
        InitializeComponent();            
    }

    public delegate void UpdateUIDelegate(string data);
    private void btnRun_Click(object sender, EventArgs e)
    {
        myProcess = new Process();
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
        myProcessStartInfo.Arguments = "test.pl";
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        myProcessStartInfo.CreateNoWindow = true;
        myProcess.StartInfo = myProcessStartInfo;
        myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);
        myProcess.Start();
        myProcess.BeginOutputReadLine(); 
    }

    void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        if (txtOutput.InvokeRequired)
        {
            UpdateUIDelegate updateDelegate = new UpdateUIDelegate(UpdateUI);                
            this.Invoke(updateDelegate, e.Data);
        }            
    }

    void UpdateUI(string data)
    {
        txtOutput.Text += data + "\r\n";
    }
}

和代码test.pl:

and code for test.pl:

my @a = qw{1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19};
my @b = qw{a b c d e f g h i j  k  l  m  n  o  p  q  r  s };
print 'start' . "\n";
while ( my ( $item1, $item2) = ( splice (@a, 0, 1), splice (@b, 0, 1) ) ) {
    print 'Item 1: ' . $item1 . "\n";
    print 'Item 2: ' . $item2 . "\n";
    warn 'Finish one item' . "\n";
    sleep(1);
}



我有一个问题是,它的输出数据仅显示在文本框,直到Perl的完成。

I have a problem is that the output data is only displayed on text box until the Perl has finished.

这时候我发现,如果我做同样的一个控制台应用程序(C#)一切似乎还好更有趣。

It's more interesting when I found that, If I do the same with a console application (C#) everything seems okay.

下面是控制台应用程序的代码:

Here's the code for console application:

class Program
{
    static void Main(string[] args)
    {
        Process myProcess = new Process();
        ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
        myProcessStartInfo.Arguments = "test.pl";
        myProcessStartInfo.UseShellExecute = false;
        myProcessStartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo = myProcessStartInfo;

        myProcess.OutputDataReceived += new DataReceivedEventHandler(myProcess_OutputDataReceived);                                    
        myProcess.Start();            
        myProcess.BeginOutputReadLine();            
        Console.Read();
    }

    static void myProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine(e.Data);
    }
}



我试图找出什么情况我窗体应用程序,但仍然没有找到任何线索。
一件事是,我不能得到警告与Windows窗体应用程序的消息。

I'm trying to figure out what happens with my form application but still not find any clue. One more thing is that I can not get warn message with windows form application.

推荐答案

您打算需要使用多个线程,因此它不会interupt你的UI。我有推出自己的线程和进程管道下放事件相当大的实用工具类。

You're going to need to use multiple threads so it doesn't interupt your UI. I have a fairly large utility class that launches processes on its own thread and pipes to delegated events.

对不起,我想举一个例子,但我在一个很实际巨大的热潮。但有一件事否则你将要注意使用Perl脚本是他们没有很好地自动刷新输出。你需要把:

Sorry, I'd give an example but I'm actually in a huge rush. But one thing else you will want to watch out for with using Perl scripts is that they do not auto flush the output nicely. You need to put:

local $| = 1;

目前你正在运行,因此自动刷新你的脚本的顶部。

At the top of your script that you're running so it auto flushes.

这篇关于我如何调用C#应用程序Perl脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆