Wie rufe ich Perl-Skript in C# - Anwendung?

Ich will die Ausgabe eines Perl-Programm und display-Daten-Ausgabe (string) auf dem Bildschirm) in ein text-Feld auf C# Windows Form.

Hier ist mein 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";
    }
}

und code für 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);
}

Ich ein problem habe, ist, dass die Ausgabe der Daten wird nur angezeigt, im Textfeld, bis das Perl-fertig.

Es ist interessanter, wenn ich fand, dass, Wenn ich das gleiche tun mit einer console-Anwendung (C#), alles scheint okay zu sein.

Hier ist der code für die console-Anwendung:

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);
    }
}

Ich versuche, herauszufinden, was passiert mit meinem form-Anwendung, aber immer noch nicht finden, keine Ahnung.
Eine weitere Sache ist, dass ich kann nicht get warn-Nachricht mit windows-form-Anwendung.

Hi! Minh, ich bin mit oben stehenden code für die Ausführung von lokalen perl-Datei und bekommen runtime-Ausgabe in der textbox wie erwartet, aber wenn ich m laufen remote-Rechner bat-Datei von der lokalen Maschine.dann die batch-Datei ist trriggered in der remote-Maschine haben, aber der output wird nicht angezeigt in der textbox. ich m geändert wird, nur diese 2 Zeile vom obigen code ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("psexec.exe"); myProcessStartInfo.Arguments = "\\\\machinename -u Domain\\usr -p pass C:\\run.bat"; pls help me out.
Haben Sie versuchen, führen Sie Ihren Befehl in der Eingabeaufforderung? wenn ja, hat es-Ausgang was erwarten Sie?
ja, gleiche Befehl psexec \\machinename -u Domain\usr -p pass c:\run.bat arbeitet Eingabeaufforderung, und geben Sie die Ausgabe wie erwartet in der Eingabeaufforderung.

InformationsquelleAutor Minh Le | 2009-03-16

Schreibe einen Kommentar