如何在dll中的另一个表单上重定向进程输出

时间:2018-05-12 08:04:33

标签: c# process

我将从delphi转到c#,我是新手c#,我的代码运行良好:

// Form1

public void Scan(string fOut)
{
    Form2 frm2;

    void SortOutputHandler(object sender, DataReceivedEventArgs e)
    {
        Trace.WriteLine(e.Data);
        this.BeginInvoke(new MethodInvoker(() =>
        {
             frm2.addItem(e.Data ?? string.Empty);
        }));
    }

    using (Process p = new Process())
    {
        frm2 = new Form2();
        frm2.Show(this);
        p.StartInfo.RedirectStandardError = true;
       // necessary code
        p.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);
        p.Start();
        p.BeginOutputReadLine();
        while (!p.HasExited)
        {
            Application.DoEvents(); 
        }
        p.Close();
        frm2.Close();
        frm2.Dispose();
     }
}

// Form2

public void addItem(string line)
{
   listBox1.Items.Add(line);
}

private void Form2_Shown(object sender, EventArgs e)
{
   listBox1.Items.Clear();
}

我想将代码移动到dll,但我找不到调用dllname.Scan(“fn.xxx”)的Form1引用:

this.BeginInvoke..

提前致谢

0 个答案:

没有答案