将db对象发送到新线程的类

时间:2013-12-03 14:18:06

标签: c# multithreading

我想将db对象发送到我的新线程的构造函数方法我该怎么做

db = DBContext.CreateInstance(ConfigurationManager.ConnectionStrings["EDocSis.DAL.EDefterDB"].ConnectionString);
    FaturaUploadTreadHelper helper = new FaturaUploadTreadHelper(kurumVeriAktarimList);
                        ThreadStart job = new ThreadStart(helper.UpdateRecords);
                        Thread thread = new Thread(job);
                        thread.Start();


/////////////////////////////////////////////////////////////////
 public string ManuelFaturaUploadDirectory
        {
            get { return ConfigurationManager.AppSettings.Get("ManuelFaturaUploadDirectory"); }
        }
        public IEDefterDB db { get; set; }
        public string RealFileName { get; set; }
        public string KurumVkn { get; set; }
        public string FileExtension { get; set; }
        public int RowCount { get; set; }
        public long ContentLength { get; set; }
        public int KurumID { get; set; }
        public int VeriAktarimID { get; set; }
        public List<EDocSis.DAL.EFatura.KurumVeriAktarim> KurumVeriAktarimlariList { get; set; }

        public FaturaUploadTreadHelper(List<EDocSis.DAL.EFatura.KurumVeriAktarim> kurumVeriAktarimlariList = null)
        {

            this.KurumVeriAktarimlariList = kurumVeriAktarimlariList;
        }

        public void UpdateRecords()
        {
            foreach (var kurumVeriAktarim in this.KurumVeriAktarimlariList)
            {
                this.VeriAktarimID = kurumVeriAktarim.ID;
                var tempFileName = Path.GetFileName(kurumVeriAktarim.DosyaBilgisi);
                this.RealFileName = tempFileName;
                var vkn = db.KurumDetaylari.First(x => x.ID == kurumVeriAktarim.Parent.ID).KimlikNo;
                this.KurumVkn = vkn;
                var extension = Path.GetExtension(kurumVeriAktarim.DosyaBilgisi);
                this.FileExtension = extension;
                this.RowCount = kurumVeriAktarim.SatirSayisi;
                this.ContentLength = kurumVeriAktarim.ToplamBuyukluk;
                this.KurumID = kurumVeriAktarim.Parent.ID;
                Run();
            }

2 个答案:

答案 0 :(得分:3)

Thread.Start方法的重载次数为object。修改方法UpdateRecords以接收对象:

UpdateRecords(object db)

然后将此行ThreadStart job = new ThreadStart(helper.UpdateRecords);更改为:

ParameterizedThreadStart job =
    new ParameterizedThreadStart(helper.UpdateRecords);

然后当你启动线程时:

thread.Start(db);

正如Matheus所说,每个线程可能都有自己的db实例,但我无法真正决定这一点。我对你的申请知之甚少(实际上不可能)。

答案 1 :(得分:2)

你可以这样做:

db = DBContext.CreateInstance(ConfigurationManager.ConnectionStrings["EDocSis.DAL.EDefterDB"].ConnectionString);
    FaturaUploadTreadHelper helper = new FaturaUploadTreadHelper(kurumVeriAktarimList);

                        Thread thread = new Thread(
                            unused => UpdateRecords(db)
                        );
                        thread.Start();

        public void UpdateRecords(object db)
        {
//DBContext for thread safe purposes.
 db = DBContext.CreateInstance(ConfigurationManager.ConnectionStrings["EDocSis.DAL.EDefterDB"].ConnectionString);

            foreach (var kurumVeriAktarim in this.KurumVeriAktarimlariList)
            {
                this.VeriAktarimID = kurumVeriAktarim.ID;
                var tempFileName = Path.GetFileName(kurumVeriAktarim.DosyaBilgisi);
                this.RealFileName = tempFileName;
                var vkn = db.KurumDetaylari.First(x => x.ID == kurumVeriAktarim.Parent.ID).KimlikNo;
                this.KurumVkn = vkn;
                var extension = Path.GetExtension(kurumVeriAktarim.DosyaBilgisi);
                this.FileExtension = extension;
                this.RowCount = kurumVeriAktarim.SatirSayisi;
                this.ContentLength = kurumVeriAktarim.ToplamBuyukluk;
                this.KurumID = kurumVeriAktarim.Parent.ID;
                Run();
            }

有一点你必须知道,DBContext不是线程安全的,即使你将对象作为参数发送,你也必须创建一个新的连接。但是将dbContext作为方法的参数发送也没有意义。