简体   繁体   English

在事件处理程序中将Process类(发送方)线程强制转换为什么的内容

[英]What to cast a Process class (sender) thread to…in event handler

For instance, a thread that is a BackgroundWorker, can be cast like: 例如,可以将一个作为BackgroundWorker的线程转换为:

private void worker_DoWork(object sender, DoWorkEventArgs e)
    {
        System.ComponentModel.BackgroundWorker senderWorker
                = sender as System.ComponentModel.BackgroundWorker;
    } 

The code above represents what I have for my Background worker thread. 上面的代码表示我对后台工作线程的处理。 I cast [sender] as a BackGround Worker - because I know thats what he is. 我将[发送者]选为背景工作者-因为我知道那是他的身份。

I can't seem to find what I should cast it to if: instead of a Background worker, what if I had used a Process class, and executed say a DOS batch file, using: 我似乎找不到如果应该将其强制转换为什么:如果不是我使用了Process类,并执行了一个DOS批处理文件,则使用:

enter code here

Process proc = new Process(); 流程proc = new Process(); proc.FileName = "some_dos_batch_file.bat"; proc.FileName =“ some_dos_batch_file.bat”; proc.Exited = ProcessExited; proc.Exited = ProcessExited; proc.Start(); proc.Start();

Sorry about syntax, but when this process completes, its completion will be handled by 'ProcessExited' below. 对不起语法,但是当此过程完成时,其完成将由下面的“ ProcessExited”处理。 But What should I cast the sender arg to in THAT case - NOT a Background Worker obviously, but I'm not sure to what? 但是在那种情况下,我应该将发送方arg强制转换为什么-显然不是后台工作人员,但是我不确定该怎么办? I would like to use the .Results property the same as I did for the Background worker. 我想使用.Results属性,就像我对Background worker一样。

Thanks - sorry for the confusion. 谢谢-抱歉造成您的困惑。

enter code here




    void ProcessExited(object sender, EventArgs e)

    {
     }

I hope I have understood your question, if not, please clarify. 希望我已经理解您的问题,否则请澄清。 If you are talking about threading and using the System.Diagnostics.Process then you would need to use Thread events...consider this below a simple class called TestARP that shells out to the command line using a hidden window to retrieve the MAC/IP address of the active connection, with the output of the command redirected to a stream which is appended to a stringbuilder instance: 如果您正在谈论线程并使用System.Diagnostics.Process那么您将需要使用Thread事件...在名为TestARP的简单类下面考虑一下此问题,该类使用隐藏窗口检索命令行到MAC / IP的外壳活动连接的地址,命令的输出重定向到附加到stringbuilder实例的流:

public class TestARP
{
    private StringBuilder sbRedirectedOutput = new StringBuilder();
    public string OutputData
    {
        get { return this.sbRedirectedOutput.ToString(); }
    }
    public void Run()
    {
        System.Diagnostics.ProcessStartInfo ps = new System.Diagnostics.ProcessStartInfo();
        ps.FileName = "arp";
        ps.ErrorDialog = false;
        ps.Arguments = "-a";
        ps.CreateNoWindow = true;
        ps.UseShellExecute = false;
        ps.RedirectStandardOutput = true;
        ps.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

        using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
        {
            proc.StartInfo = ps;
            proc.Exited += new EventHandler(proc_Exited);
            proc.OutputDataReceived += new System.Diagnostics.DataReceivedEventHandler(proc_OutputDataReceived);
            proc.Start();
            proc.WaitForExit();
            proc.BeginOutputReadLine();
            while (!proc.HasExited) ;
        }
    }

    void proc_Exited(object sender, EventArgs e)
    {
        System.Diagnostics.Debug.WriteLine("proc_Exited: Process Ended");
    }

    void proc_OutputDataReceived(object sender, System.Diagnostics.DataReceivedEventArgs e)
    {
        if (e.Data != null) this.sbRedirectedOutput.Append(e.Data + Environment.NewLine);
        //System.Diagnostics.Debug.WriteLine("proc_OutputDataReceived: Data: " + e.Data);
    }
}

If you were to run this in a thread the Process's events will still get caught ( only on the thread itself ), but if you're talking about waiting for the thread to finish, look at this class code here called ThreadTestARP that runs the above class on a thread... 如果要在线程中运行该进程的事件仍然会被捕获( 仅在线程本身上 ),但是如果您正在谈论等待线程完成,请在此处运行此类的名为ThreadTestARP此类代码线程上的类...

public class ThreadTestARP
{
    private TestARP _testARP = new TestARP();
    private ManualResetEvent _mre = new ManualResetEvent(false);
    public ThreadTestARP()
    {
    }
    public TestARP ARPTest
    {
        get { return this._testARP; }
    }
    public void Run()
    {
        Thread t = new Thread(new ThreadStart(RunThread));
        t.Start();
        this._mre.WaitOne();
        // Blocks here...
        t.Join();
    }
    private void RunThread()
    {
        this._testARP.Run();
        this._mre.Set();
    }
}

Note how the ManualResetEvent _mre is used to signal to say in the context of the thread, "right, I am done, back to the creator..." 请注意,在线程上下文中,如何使用ManualResetEvent _mre发出信号说:“对,我做完了,回到创建者...”

Why can't you cast to a Process object? 为什么不能转换为Process对象? You can still access some members of Process objects, such as ExitCode or ExitTime, that have terminated. 您仍然可以访问已终止的Process对象的某些成员,例如ExitCode或ExitTime。

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx http://msdn.microsoft.com/zh-CN/library/system.diagnostics.process.exited.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM