如何知道应用程序何时启动?

时间:2017-10-11 07:12:20

标签: c#

  • 我正在用C#创建一个用于监控某些应用程序的应用程序,所以如何知道exe运行的时间(例如:记事本)。

  • 我尝试每1秒使用timer运行检查if (Process.GetProcessesByName("process_name").Length > 0),但可能不是好方法。

2 个答案:

答案 0 :(得分:0)

我认为最好的方法是设置计时器,它将检查每个运行进程,比方说,5秒。在计时器// This will return an empty array if notepad isn't running. Process[] localByName = Process.GetProcessesByName("notepad"); 事件中,按名称

检查运行进程
items:[{
    xtype: "combobox",
    name: "TubeWellId",
    fieldLabel: "Tubewell",
    store: tube_well_store,
    allowBlank: false,
    displayField: "TubeWellName",
    valueField: "TubeWellId",
    queryMode: "local",
    forceSelection: true,
    listeners: {
        select: function (combo) {
            var getForm = this.up("form").getForm();
            if (combo.getValue() === 1) {
                getForm.findField("TubeWellDistance").setReadOnly(true);
            } else {
                getForm.findField("TubeWellDistance").setReadOnly(false);
            }
        }
    }
}, {
    xtype: "combobox",
    name: "TubeWellDistance",
    fieldLabel: "Tubewell Distance",
    store: tube_well_store,
    allowBlank: false,
    displayField: "DistanceName",
    valueField: "DistanceId",
    queryMode: "local",
    forceSelection: true,
}]

看看MSDN documentation

答案 1 :(得分:0)

我为你建造一个同样的应用程序。这是我的解决方案:

public void findProcess(string processName)
    {
        BackgroundWorker bg = new BackgroundWorker();
        bg.DoWork += (object sender, DoWorkEventArgs e){
            while(true)
            {
                Process[] process = Process.GetProcesses();
                foreach(var p in process )
                    if(p.ProcessName.Equals(processName))
                    {
                        // TODO:
                    }

                Thread.Sleep(1000);
            }            
        };
    }
相关问题