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();
            }
8
  • Each thread should have their own DB instance. Why don't you passes the connection string, instead? Commented Dec 3, 2013 at 14:20
  • so i should create db object in constructor method for each thread is it true ? Commented Dec 3, 2013 at 14:21
  • Not in the constructor, in the execution method. Commented Dec 3, 2013 at 14:22
  • can you write some code? Commented Dec 3, 2013 at 14:23
  • As @Michael said, we can't dictate anything since we don't know if DBContext is thread safe. Usually, they don't, but who knows? Commented Dec 3, 2013 at 14:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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);
@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.
@user990513 OP needs to change ThreadStart to ParametrizedThreadStart
@MatheusFreitas, fantastic addition my friend! Just fantastic. I'll update the answer.
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.

4 Comments

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;
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.
sending dbContext is a bad approach ? is it True? if is it true i will create a db connection in my thread method
if you are using threads then yes it is a bad approach to send the dbContext as a parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.