static List<cs> lstDB= new List<cs>(); //用来数据库操作的队列
private void btnqd_Click(object sender, EventArgs e)
{
t1 = new Thread(new ThreadStart(Calculate));
t1.IsBackground = true;
t1.Start(); //启动新线程
// 再启动一个线程 读取 lstdb中信息 进行写库操作
t2 = new Thread(new ThreadStart(xk));
t2.IsBackground = true;
t2.Start();
}public void Calculate()//线程1:获取数据并存入队列
{
msg = item.GetOne();//获取的数据
if(msg.Mlz == "BR000")
{
c = new cs();
c.dbMlz = "BR000";
c.strphone = item.Phone;
c.strxxt = msg.Xxt;
AddDb(c);
}
}void AddDb(cs value)
{
lock (lstDB)
{
lstDB.Add(value);
}
}
struct cs
{
public string dbMlz;//标识
public string strphone;//手机号
public string strxxt;//信息体
public string strmlz;//命令字
}public void xk()//线程2:想通过队列获取数据写库
{
while(true)
{
//这里有问题
}
}
上面是主要代码,我想在xk方法里锁定lstDB提取数据,并写入数据库,如何写能减少锁定时间,并优化程序?
private void btnqd_Click(object sender, EventArgs e)
{
t1 = new Thread(new ThreadStart(Calculate));
t1.IsBackground = true;
t1.Start(); //启动新线程
// 再启动一个线程 读取 lstdb中信息 进行写库操作
t2 = new Thread(new ThreadStart(xk));
t2.IsBackground = true;
t2.Start();
}public void Calculate()//线程1:获取数据并存入队列
{
msg = item.GetOne();//获取的数据
if(msg.Mlz == "BR000")
{
c = new cs();
c.dbMlz = "BR000";
c.strphone = item.Phone;
c.strxxt = msg.Xxt;
AddDb(c);
}
}void AddDb(cs value)
{
lock (lstDB)
{
lstDB.Add(value);
}
}
struct cs
{
public string dbMlz;//标识
public string strphone;//手机号
public string strxxt;//信息体
public string strmlz;//命令字
}public void xk()//线程2:想通过队列获取数据写库
{
while(true)
{
//这里有问题
}
}
上面是主要代码,我想在xk方法里锁定lstDB提取数据,并写入数据库,如何写能减少锁定时间,并优化程序?
{
while(true)
{
//这里有问题
//类似queue,每次取一个值,如果值部位空,写入数据库。
}
}