110,538
社区成员
发帖
与我相关
我的任务
分享
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 多线程练习
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataTable dt;
int i=0;
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable("mytable");
dt.Columns.Add("num1",typeof(Int32));
dt.Columns.Add("num2",typeof(Int32));
dt.Columns.Add("co", typeof(Int32));
DataRow crow;
for (int n = 1; n <=1000;n++ )
{
crow = dt.NewRow();
crow["num1"] = n;
crow["num2"] = n + 1;
crow["co"] = 0;
dt.Rows.Add(crow);
}
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
Thread threads;
for (int t = 0; t < 10;t++)
{
threads = new Thread(new ThreadStart(Dodail));
threads.IsBackground = true;
threads.Name=t.ToString()+"号线程";
threads.Start();
}
}
private void Dodail()
{
while(i<1000)
{
}
}
}
}
@Z65443344 我之前也想过线程池,但是我在网上查了一下,线程池主要是处理耗时比较短的线程,这里可能是耗时比较短,但是如果我是抓取网页的话,那就不一样了。而且如果我中用10个或者几个线程的话,用线程池效率不高,所以我才想用这种方式去做。
这是之前我提的一个问题没有解决,现在我把完整代码发出来,求高手解决。 要求:datagridview里面,第一列数据加上第二列数据,然后把结果返回到第三列,用10个线程处理。 下面是界面: