通过定时器在ListView中删除项目
本文关键字:删除项目 ListView 定时器 | 更新日期: 2023-09-27 18:18:25
我有一个c# ListView与一些条目,一个方法来删除第一个条目和一个定时器来调用这个方法。我的问题是,计时器工作得很好(我通过调用MessageBox检查了这一点),并且删除方法也很好(我通过使用按钮调用此方法而不是计时器来检查此方法)。但是定时器仍然不能从我的ListView中删除项目。
我代码: void Button1Click(object sender, EventArgs e)
{
removeItems();
}
private void timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
removeItems();
}
void removeItems()
{
MessageBox.Show("Hello from the removeMethod");
listViewTeam.Items.RemoveAt(0);
}
两次调用removeItems();让消息框出现,但只有按钮,让也删除listView的第一个项目。
有人可以帮助我如何我可以删除第一个项目的计时器?
您正在使用的计时器不是线程安全的。而不是使用System.Timer
,你应该使用System.Windows.Forms.Timer
,因为它自动运行在UI线程上。然后你的代码将完美地工作。
您似乎正在使用System.Timer
,这意味着您的Elapsed
回调可能不一定在UI线程上调用。您可以通过使用Invoke
(即
if (listViewTeam.InvokeRequired)
{
listViewTeam.Invoke((MethodDelegate)delegate { listViewTeam.Items.RemoveAt(0); });
}
或者更简单地设置定时器的SynchronizingObject属性为包含ListView
的表单,您的代码将正常工作
你需要使用delegate
从后台线程安全地与UI控件交互(这是你真正在你实现Timer
时所做的):
void Button1Click(object sender, EventArgs e)
{
removeItems();
}
private void timer_Tick(object sender, System.Timers.ElapsedEventArgs e)
{
removeItems();
}
void removeItems()
{
MessageBox.Show("Hello from the removeMethod");
RemoveListViewItem(0);
}
public delegate void InvokeRemoveListViewItem(Int32 ItemIndex);
public void RemoveListViewItem(Int32 ItemIndex)
{
if (InvokeRequired)
{
try { Invoke(new InvokeRemoveListViewItem(RemoveListViewItem), new Object[] { ItemIndex }); }
catch (Exception)
{
//react to the exception you've caught
}
}
listView.RemoveAt(ItemIndex);
}
就像SLC说的,你需要使用BeginInvoke。我个人是这样解决的
public AddListItem myDelegate;
你应该检查:
- 几秒钟后关闭消息框
- http://www.codeproject.com/Articles/19714/Auto-close-message-box
- http://www.codeproject.com/Articles/7968/MessageBox-with-a-timeout-for-NET
Windows.Forms System.Timers System.Threading
Timer event runs on what thread? UI thread UI or worker thread Worker thread
Instances are thread safe? No Yes No
Familiar/intuitive object model? Yes Yes No
Requires Windows Forms? Yes No No
Metronome-quality beat? No Yes* Yes*
Timer event supports state object? No No Yes
Initial timer event schedulable? No No Yes
Class supports inheritance? Yes Yes No
* Depending on the availability of system resources (for example, worker threads)
先启动计时器。但是请使用Windows窗体定时器。
你的设计器类:
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listBox1 = new System.Windows.Forms.ListBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object[] {
"q",
"w",
"e",
"r",
"t",
"y",
"u",
"sfdgsdf",
"gf",
"gsd",
"fgs",
"dfg"});
this.listBox1.Location = new System.Drawing.Point(170, 200);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 0;
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(570, 502);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Timer timer1;
}
你的表单类:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
removeItems();
if (listBox1.Items.Count == 0)
{
timer1.Stop();
}
}
void removeItems()
{
MessageBox.Show("Hello from the removeMethod");
listBox1.Items.RemoveAt(0);
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}