UPusing System;
using System.Drawing;
using System.Text;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;namespace listviewslow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void AddlistDelegate(string strEntry, int indexnum); private void Addlist(string strEntry, int indexnum)
{ if (InvokeRequired)
{
//marshall the call into the main UI thread
BeginInvoke(new AddlistDelegate(Addlist), new object[] { strEntry, indexnum });
return;
}
ListViewItem item = new ListViewItem(Convert.ToString(indexnum));
item.SubItems.Add(Convert.ToString(strEntry));
resultlist.Items.Add(item); }
//-------------------
private void button1_Click(object sender, EventArgs e)
{
int id = 1;
int endid = 1000;
const int nThreadNumber = 15; //Thread Number State stateInfo; int nIndex = 0;
for (int i = 0; i < nThreadNumber; i++)
{
if (id == endid)
break;
id++;
string StrID = id.ToString();
nIndex++;
stateInfo =
new State(StrID, nIndex ); ThreadPool.QueueUserWorkItem(new WaitCallback(
DoSomething), stateInfo); Thread.Sleep(10);
}
//one free, one create
while (true)
{
if (id == endid)
break;
id++;
string StrID = id.ToString();
nIndex++; if (nIndex % 300 == 0)
{
Thread.Sleep(300);
}
stateInfo =
new State(StrID, nIndex );
ThreadPool.QueueUserWorkItem(new WaitCallback(
DoSomething), stateInfo);
Thread.Sleep(10);
} }
public void DoSomething(object state)
{
// Get parameter State st = (State)state;
string StrID = st.strIDNum;
int nIndex = st.nIndex;
if (nIndex % 5 == 0)
{
Addlist("是5的倍数", nIndex);
}
else
{
Addlist("不是5的倍数", nIndex);
}
}
}
public class State
{
public string strIDNum;
public int nIndex;
public State(string strIDNum, int nIndex)
{
this.strIDNum = strIDNum;
this.nIndex = nIndex;
}
}}
using System.Drawing;
using System.Text;
using System.Security.Permissions;
using System.Threading;
using System.Windows.Forms;namespace listviewslow
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void AddlistDelegate(string strEntry, int indexnum); private void Addlist(string strEntry, int indexnum)
{ if (InvokeRequired)
{
//marshall the call into the main UI thread
BeginInvoke(new AddlistDelegate(Addlist), new object[] { strEntry, indexnum });
return;
}
ListViewItem item = new ListViewItem(Convert.ToString(indexnum));
item.SubItems.Add(Convert.ToString(strEntry));
resultlist.Items.Add(item); }
//-------------------
private void button1_Click(object sender, EventArgs e)
{
int id = 1;
int endid = 1000;
const int nThreadNumber = 15; //Thread Number State stateInfo; int nIndex = 0;
for (int i = 0; i < nThreadNumber; i++)
{
if (id == endid)
break;
id++;
string StrID = id.ToString();
nIndex++;
stateInfo =
new State(StrID, nIndex ); ThreadPool.QueueUserWorkItem(new WaitCallback(
DoSomething), stateInfo); Thread.Sleep(10);
}
//one free, one create
while (true)
{
if (id == endid)
break;
id++;
string StrID = id.ToString();
nIndex++; if (nIndex % 300 == 0)
{
Thread.Sleep(300);
}
stateInfo =
new State(StrID, nIndex );
ThreadPool.QueueUserWorkItem(new WaitCallback(
DoSomething), stateInfo);
Thread.Sleep(10);
} }
public void DoSomething(object state)
{
// Get parameter State st = (State)state;
string StrID = st.strIDNum;
int nIndex = st.nIndex;
if (nIndex % 5 == 0)
{
Addlist("是5的倍数", nIndex);
}
else
{
Addlist("不是5的倍数", nIndex);
}
}
}
public class State
{
public string strIDNum;
public int nIndex;
public State(string strIDNum, int nIndex)
{
this.strIDNum = strIDNum;
this.nIndex = nIndex;
}
}}
解决方案 »
- 如何让一个文件夹里所有的MP3文件都显示在一个listbox
- 求教如何取Appconfig的值
- 装箱与拆箱的疑问
- 如何在控制台下显示18030字符
- 请高手指教
- 菜鸟问题:如何判断一个字符串是否符合日期类型的格式~!!!快来抢分啊,先来先得,利马结贴!!!!
- [求助]关于Nasa World Wind 源码
- 用java和C#去做Web,或WebGIS,谁更有优势?
- wpf怎么把checkbox放到combobox里面..
- 我在网上下了个程序。但调试的时候提示说:“无法在Web服务器上启动调试,因为没有起用集成Windows身份认证”
- 求一ASP.NET_C#可用的通用缓存类
- 每次运行时候都出现同样一个问题怎么解决?
建议把整个循环都封装到一个方法中后再异步执行。
在添加完成一个Item后加上
Application.DoEvent();