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;
}
}}
解决方案 »
- 求提取迅雷看看.xv视频的C#代码或者详细思路谢谢
- 用C#打开文件对话框,我设置了过滤条件为何还是全部显示出来?
- 针对PDA使用MainMenu
- 有没有遇到打印报表出现隔张打印的朋友
- C# 多线程的问题,请高手解答下
- 老问题新问,还是DG排序问题,照例送上满分100
- 如何用正则表达式 替换book.comment.list.jsp_book_no=142545&page_no=1中的.jsp后边的下横 线 '_'为'?'
- 在VS.NET中如何設置讓程序能生成XML幫助?
- 窗口透明问题
- RichTextBox的拖放问题
- 求一ASP.NET_C#可用的通用缓存类
- 每次运行时候都出现同样一个问题怎么解决?
建议把整个循环都封装到一个方法中后再异步执行。
在添加完成一个Item后加上
Application.DoEvent();