using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
ThreadStart thread = null;
Thread thread1 = null;
DataTable dt = null;
private void button1_Click(object sender, EventArgs e)
{
string sql = "select top 1000 id,content from articleinfo";
dt = DBHelper.GetDataSet(sql);
startThread();
}
public void startThread()
{
thread = new ThreadStart(addnum);
Thread[] th = new Thread[5];
for (int j = 0; j < th.Length; j++)
{
th[j] = new Thread(thread);
th[j].IsBackground = true;
th[j].Start(); }
thread1 = new Thread(new ThreadStart(DshowNum));
thread1.IsBackground = true;
thread1.Start();
}
public void addnum()
{
foreach (DataRow dr in dt.Rows)
{
string id = dr["id"].ToString();
string content = dr["content"].ToString();
lock (this)
{
InsertData(id,content); // 每个线程都会执行一次
i++;
}
}
} public void InsertData(string id,string content)
{
try
{
string sql = "insert into articleInfo2(id,content)values('" + id + "','" + content + "')";
DBHelper.ExecuteNonQuery(sql);
}
catch (Exception ex)
{ }
}
public void DshowNum()
{
MethodInvoker mi = new MethodInvoker(showNum);
while (true)
{
Invoke(mi);
}
} public void showNum()
{
this.label1.Text = "这是第" + i + "个数";
}
}
}大侠帮我看一下,在注释的那个方法处,每个线程都会执行一次,怎样才能让其中一个线程访问,别的线程处理下一条数据
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 0;
ThreadStart thread = null;
Thread thread1 = null;
DataTable dt = null;
private void button1_Click(object sender, EventArgs e)
{
string sql = "select top 1000 id,content from articleinfo";
dt = DBHelper.GetDataSet(sql);
startThread();
}
public void startThread()
{
thread = new ThreadStart(addnum);
Thread[] th = new Thread[5];
for (int j = 0; j < th.Length; j++)
{
th[j] = new Thread(thread);
th[j].IsBackground = true;
th[j].Start(); }
thread1 = new Thread(new ThreadStart(DshowNum));
thread1.IsBackground = true;
thread1.Start();
}
public void addnum()
{
foreach (DataRow dr in dt.Rows)
{
string id = dr["id"].ToString();
string content = dr["content"].ToString();
lock (this)
{
InsertData(id,content); // 每个线程都会执行一次
i++;
}
}
} public void InsertData(string id,string content)
{
try
{
string sql = "insert into articleInfo2(id,content)values('" + id + "','" + content + "')";
DBHelper.ExecuteNonQuery(sql);
}
catch (Exception ex)
{ }
}
public void DshowNum()
{
MethodInvoker mi = new MethodInvoker(showNum);
while (true)
{
Invoke(mi);
}
} public void showNum()
{
this.label1.Text = "这是第" + i + "个数";
}
}
}大侠帮我看一下,在注释的那个方法处,每个线程都会执行一次,怎样才能让其中一个线程访问,别的线程处理下一条数据
解决方案 »
- 求ClickOne技术支持
- 关于一个dropdownlist实现多级绑定 怎么弄
- 关于Gridview中按钮的设置
- showdialog 后如何再次显示及checkedlistbox 设置每个项的高度
- SerialPort接收数据问题
- 24时制与12时制转换问题?
- 开心智力题:十枚硬币排5直排.每排4枚硬币,怎么排?
- c#一个程序,准备做的,请大家抛玉引砖
- C# 多线程中Invoke更新TextBox信息时不显示
- 申请了hotmail的e-mail,然后登陆,为什么这样:下面的电子邮件地址没有被注册为 .NET Passport?
- WCF和Socket
- 服务端Host兼UI实现,双工通信,WCF学习
int count = dt.Rows.Cout;
int i = 0;
然后addnum每次取出一个currentIndex并行处理 public void addnum()
{
while (true)
{
int currentIndex = -1;
if (i < count)
{
lock (this)
{
if (i < count)
{
currentIndex = i;
i = i + 1;
}
else
{
break;
}
}
if (currentIndex != -1)
{
string id =dt.Rows[currentIndex]["id"].ToString();
string content = dt.Rows[currentIndex]["content"].ToString();
InsertData(id, content);
} }
else
{
break;
}
}
}