首页 > 解决方案 > 如何在 dll 中的另一个表单上重定向进程输出

问题描述

我要从 delphi 到 c#,我是一个新手 c#,我的代码运行良好:

//表格1

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

//表格2

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..

提前致谢

标签: c#process

解决方案


推荐阅读