2

i want to send db object to my new thread's constructor method how can i do

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();
            }
altandogan
  • 1,245
  • 6
  • 21
  • 44

2 Answers2

3

The Thread.Start method has an overload that receives an object. Modify your method UpdateRecords to receive an object:

UpdateRecords(object db)

then change this line ThreadStart job = new ThreadStart(helper.UpdateRecords); to this:

ParameterizedThreadStart job =
    new ParameterizedThreadStart(helper.UpdateRecords);

and then when you start the thread:

thread.Start(db);

As stated by Matheus, it's likely that each thread should have its own db instance, but I can't really dictate that. I don't know near enough (and honestly couldn't) about your application.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • i don't TreadStart methods want to any void method when i give a parameter objec db it don't accept it understand ThreadStart job = new ThreadStart(helper.UpdateRecords); Thread thread = new Thread(job); thread.Start(db); – altandogan Dec 03 '13 at 14:35
  • @user990513, I'm not understanding what you're saying. The method that you're starting on a new thread, `UpdateRecords`, can be modified to receive the `db` object. It just needs to have a parameter typed as an `object`. You'd clearly have to cast it inside of there. Then, when you issue `Thread.Start`, just pass that object into it. – Mike Perrenoud Dec 03 '13 at 14:41
  • 2
    @user990513 OP needs to change ThreadStart to [ParametrizedThreadStart](http://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart(v=vs.110).aspx) – EProgrammerNotFound Dec 03 '13 at 14:42
  • @MatheusFreitas, fantastic addition my friend! Just fantastic. I'll update the answer. – Mike Perrenoud Dec 03 '13 at 14:43
2

You can do it like that:

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();
            }

One thing you must know that, DBContext is not thread safe, even you send your object as a parameter, you have to create a new connection. But also it doesn't make sense to send your dbContext as a parameter for your method.

Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
  • i am doing this but i receive that error :The ObjectContext instance has been disposed and can no longer be used for operations that require a connection. var vkn = db.KurumDetaylari.First(x => x.ID == kurumVeriAktarim.Parent.ID).KimlikNo; – altandogan Dec 03 '13 at 14:55
  • One thing you must know that, DBContext is not thread safe, even you send your object a parameter, you have create a new connection. But also it doesn't make sense to send your dbContext as a parameter for your method. – Bura Chuhadar Dec 03 '13 at 15:05
  • sending dbContext is a bad approach ? is it True? if is it true i will create a db connection in my thread method – altandogan Dec 03 '13 at 15:08
  • 1
    if you are using threads then yes it is a bad approach to send the dbContext as a parameter. – Bura Chuhadar Dec 03 '13 at 15:15