使用Timer来检测更改

本文关键字:检测 Timer 使用 | 更新日期: 2023-09-27 18:15:00

更新:我添加了我的代码来显示整个过程,这是我上一个问题的延续

我在我的listview中列出了3个项目和一个if语句,该语句指出,如果我的subitemInactive代码里面将生成一个文件。我的问题是,一旦Inactive项目/s再次是Active,我将如何使我的计时器再次移动?

private void Form1_Load(object sender, EventArgs e)
{
    timer2.Enabled = true;
}
    private void running_process()
{
        Process[] processes = Process.GetProcesses("ITWORKSPC152");
        for (int i = 0; i < listView1.Items.Count; i++)
        {
            if (flag == false)
            {
                listView1.Items[i].SubItems.Add("Inactive");
                if(i == listView1.Items.Count - 1)
                    flag = true;
            }
            foreach (Process p in processes)
            {
                if (!listBox1.Items.Contains(listView1.Items[i].Text))
                {
                    listView1.Items[i].SubItems[1].Text = " ";
                    listView1.Items[i].SubItems[1].Text = "Inactive";
                    listView1.Items[i].BackColor = Color.Red;
                }
                if (listView1.Items[i].Text == p.ProcessName)
                {
                    listBox1.Items.Add(p.ProcessName);
                    listView1.Items[i].SubItems[1].Text = "Inactive";
                    for (int j = 0; j < listBox1.Items.Count; j++)
                    {
                        if (listBox1.Items[j].ToString() == listView1.Items[i].Text)
                        {
                            listView1.Items[i].SubItems[1].Text = "Active";
                            listView1.Items[i].BackColor = Color.FromArgb(66, 181, 33);
                            //m_boolIsDown = false;
                        }
                    }
                }
             }
          }
  }
private void InactiveCheck()
{
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        if (listView1.Items[i].SubItems[1].Text == "Inactive")
        {
            richTextBox1.Text = richTextBox1.Text + listView1.Items[i].Text + 
                " was inactive at " + DateTime.Now.ToString("hh':'mm tt") + "'n";
            File.AppendAllText(@"C:'Documents and Settings'pamojica'My Documents'InactiveProgramLogs'" + lbl_date.Text + ".txt", richTextBox1.Text);
            timer3.Enabled = false;
        }
        else
        {
            timer3.Enabled = true;
        }
    }
}
private void timer1_Tick(object sender, EventArgs e)
{
    listBox1.Items.Clear();
    richTextBox1.Clear();
    running_process();
}
private void button2_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
}
    private void timer3_Tick(object sender, EventArgs e)
    {
        InactiveCheck();
    }
    private void listView1_AfterLabelEdit(object sender, LabelEditEventArgs e)
    {
        for (int i = 0; i < listView1.Items.Count; i++)
        {
            if (listView1.Items[i] == null || listView1.Items.Count == 0)
                return;
            var item = listView1.Items[i];
            if (item.SubItems != null && item.SubItems.Count > 1 && item.SubItems[1].Text == "Inactive")
            {
                richTextBox1.Text = richTextBox1.Text + item.Text + " was inactive at " + DateTime.Now.ToString("hh':'mm tt") + "'n";
                File.AppendAllText(@"C:'Documents and Settings'pamojica'My Documents'InactiveProgramLogs'" + lbl_date.Text + ".txt", richTextBox1.Text);
            }
        }
    }

使用Timer来检测更改

根本不需要Timer。在您的情况下,问题是:当列表视图中项目的值发生变化时,您想要做一些事情。"event"是这里的关键词。

订阅ListView的一个事件,在这里选择一个。我认为,在该列表中的事件"AfterLabelEdit"是一个你正在寻找。

这里有一些提示,也许你应该根据你的具体环境调整代码:

listView1.AfterLabelEdit += (o, e) =>
{
    if (listView1.SelectedItems == null ||  listView1.SelectedItems.Count == 0)
        return;
    var item = listView1.SelectedItems[0];
    if (item.SubItems != null && item.SubItems.Count > 1 && item.SubItems[1].Text == "Inactive")
    {
        richTextBox1.Text = richTextBox1.Text + item.Text + " was inactive at " + DateTime.Now.ToString("hh':'mm tt") + "'n";
        File.AppendAllText(@"C:'Documents and Settings'pamojica'My Documents'InactiveProgramLogs'" + lbl_date.Text + ".txt", richTextBox1.Text);
    }
};

一般来说,几乎没有需要计时器的简单UI处理情况。只需使用event。

在"ListView point view"中,使用计时器是这样的:"如果有什么变化,我会看看周围。如果没有,我会在几毫秒后重新检查。我会很忙的。"

事件的(更好的)逻辑是:"嘿,控制系统。你们所有人:当事情发生变化时……随时通知我!"

不做timer3.Enabled = true;timer3.Enabled =false使用timer3.Start();timer3.Stop()

停止定时器在Tick Event将做的伎俩

private void Form1_Load(object sender, EventArgs e)
{
    timer3.Start();
}
private void InactiveCheck()
{
    for (int i = 0; i < listView1.Items.Count; i++)
    {
        if (listView1.Items[i].SubItems[1].Text == "Inactive")
        {
            richTextBox1.Text = richTextBox1.Text + listView1.Items[i].Text + " was inactive at " + DateTime.Now.ToString("hh':'mm tt") + "'n";
            File.AppendAllText(@"C:'Documents and Settings'pamojica'My Documents'InactiveProgramLogs'" + lbl_date.Text + ".txt", richTextBox1.Text);               
        }
        else
        {
            timer3.Start();
        }
    }
}
private void timer3_Tick(object sender, EventArgs e)
{
    timer3.Stop()
    InactiveCheck();
}

在ItemSelectionChanged事件中使用timer3.Stop();timer3.Start();方法

    private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
    {
        if (e.Item == sender)
        {
            if (e.IsSelected) timer3.Start();
            else timer3.Stop();
        }                        
    }