|
发表于 2013-6-25 11:13:34
|
显示全部楼层
本帖最后由 风雅。 于 2013-6-25 11:16 编辑
[code=csharp]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread _thread;
private void btn_Start_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txt_Num.Text))
{
for (int i = 0; i < int.Parse(txt_Num.Text); i++)
{
Application.DoEvents();
addListView(i.ToString(), Color.Red, "线程执行次数为:" + i.ToString());
}
}
}
private void Deter(MethodInvoker method)
{
if (InvokeRequired)
{
Invoke(method);
}
else
method();
}
private void addListView(string str, Color clo, string count)
{
try
{
_thread = new Thread(new ThreadStart(() =>
{
Deter(() =>
{
ListViewItem list = new ListViewItem();
list.SubItems.Add("时间");
list.SubItems.Add("信息");
list.SubItems[0].Text = DateTime.Now.ToString("yyyy-HH:mm");
list.SubItems[1].Text = str;
list.SubItems[2].Text = count;
list.ForeColor = clo;
lstView.Items.Insert(0, list);
});
}));
_thread.Start();
}
catch (Exception)
{
throw;
}
finally { }
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
_thread.Abort();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
}
}
[/code] |
|