【发布时间】:2011-04-03 16:09:49
【问题描述】:
我的代码是用 C# 编写的,数据层使用 LINQ to SQL 填充/加载分离的对象类。
我最近更改了代码以使用多线程,我很确定我的 DAL 不是线程安全的。
你能告诉我 PopCall() 和 Count() 是否是线程安全的,如果不是,我该如何修复它们?
public class DAL
{
//read one Call item from database and delete same item from database.
public static OCall PopCall()
{
using (var db = new MyDataContext())
{
var fc = (from c in db.Calls where c.Called == false select c).FirstOrDefault();
OCall call = FillOCall(fc);
if (fc != null)
{
db.Calls.DeleteOnSubmit(fc);
db.SubmitChanges();
}
return call;
}
}
public static int Count()
{
using (var db = new MyDataContext())
{
return (from c in db.Calls select c.ID).Count();
}
}
private static OCall FillOCall(Model.Call c)
{
if (c != null)
return new OCall { ID = c.ID, Caller = c.Caller, Called = c.Called };
else return null;
}
}
分离的 OCall 类:
public class OCall
{
public int ID { get; set; }
public string Caller { get; set; }
public bool Called { get; set; }
}
【问题讨论】:
标签: c# .net multithreading linq-to-sql