gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 12:49:15 24 4
gpt4 key购买 nike

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

最佳答案

Thread.Start 方法具有接收对象的重载。修改您的方法 UpdateRecords 以接收对象:

UpdateRecords(object db)

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

ParameterizedThreadStart job =
new ParameterizedThreadStart(helper.UpdateRecords);

然后当你启动线程时:

thread.Start(db);

正如 Matheus 所说,每个线程很可能应该有自己的 db 实例,但我无法真正决定这一点。我对您的申请了解不够(老实说也不能)。

关于c# - 将 db 对象发送到新线程的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20353588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com