我怎样才能获得最后一个运行过程

时间:2016-10-24 18:50:56

标签: c#

Process[] processList1 = Process.GetProcesses();
int size = processList1.Length;

MessageBox.Show("" + processList1[size - 1]);

3 个答案:

答案 0 :(得分:1)

如果您正在谈论最近的流程,可以通过按开始时间排序列表来实现:

Process mostRecent = Process.GetProcesses().OrderBy(p => p.StartTime).Last();

答案 1 :(得分:0)

试试这个:

  public partial class Form1 : Form
{
    Process[] processList1;
    public Form1()
    {
        InitializeComponent();
        processList1 = Process.GetProcesses();
    }
   //set timer1.Interval to 5000

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (Process item in Process.GetProcesses())
        {
            if (!processList1.Contains(item))
            {
                //get all new Processes
            }
        }
        processList1 = Process.GetProcesses();
    }
}

答案 2 :(得分:0)

使用此代码。 此代码获取所有进程并比较开始日期,如果您打开一个新进程,它将在label1中显示它应该在您的表单中的名称。

    private void timer1_Tick(object sender, EventArgs e)
    {
        System.Diagnostics.Process[] processList1 = System.Diagnostics.Process.GetProcesses();
        DateTime min = processList1[0].StartTime;

        Process last = new Process();
        foreach (Process a in processList1)
        {

            try
            {
                if (DateTime.Compare(a.StartTime, min) >= 0)
                {
                    last = a;
                    min = a.StartTime;
                }

            }
            catch (Exception)
            {

            }

        }
        label1.Text = last.ProcessName;
    }