[C#] 纯文本查看 复制代码 using System;
using System.Threading;
using System.Windows.Forms;
namespace Text1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.listView1.View = View.Details;
this.listView1.HeaderStyle = ColumnHeaderStyle.Clickable;
this.listView1.Columns.Add("n1", -2);
}
private void button1_Click(object sender, EventArgs e)
{
String Name = (sender as Button).Name;
switch(Name)
{
case "button1": Thr_方法1(Name); break;
case "button2": Thr_方法2(Name); break;
}
}
delegate void Delegate_ListViewadd(string Str1);
void Thr_方法1(String input)
{
Thread thread = new Thread(Thr_方法1_B);
thread.IsBackground = true;
thread.Start(input);
}
void Thr_方法1_B(object input)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
this.Invoke((EventHandler)(delegate
{
listView1.Items.Add(input as string + ":" + i);
listView1.Items[listView1.Items.Count - 1].EnsureVisible();
}));
}
}
void Thr_方法2(String input)
{
Thread thread = new Thread(Thr_方法2_A);
thread.IsBackground = true;
thread.Start(input);
}
void Thr_方法2_A(object input)
{
Delegate_方法2_B的委托 s1 = new Delegate_方法2_B的委托(Thr_方法2_B);
for (int i = 0; i < 100; i++)
{
Thread.Sleep(1000);
this.Invoke(s1, input as string + ":" + i);
}
}
delegate void Delegate_方法2_B的委托(String Str1);
void Thr_方法2_B(String Str1)
{
listView1.Items.Add(Str1);
listView1.Items[listView1.Items.Count - 1].EnsureVisible();
}
}
} |