使用Timer检测更改

时间:2014-07-25 06:52:48

标签: c# winforms

更新:我添加了我的代码以显示整个过程,这在某种程度上延续了我的last question

我的listview中列出了3个项目和if语句,其中声明如果我的subitemInactive代码,则会生成一个文件。我的问题是,一旦Inactive项目再次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);
            }
        }

    }

3 个答案:

答案 0 :(得分:3)

根本不需要计时器。在您的情况下,问题是:当列表视图中的项目值发生变化时,您希望执行某些操作。 &#34;事件&#34;这是关键词。

订阅ListView的活动,选择one here。我认为在该列表中的事件&#34; AfterLabelEdit&#34;是你要找的那个。

以下是一些线索,也许您应该根据具体情况调整代码:

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手册。只是使用事件。

在&#34; ListView的观点&#34;中,使用计时器就像: &#34;如果发生了变化,我会环顾四周。如果没有,我将在几毫秒内重新检查。我会很忙。&#34;

事件的(更好)逻辑是: &#34;嘿你,控制。各位:当事情发生变化时......请留意我!&#34;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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