我有这个表单,我在数据库上执行许多操作。我从数据库中获取了大量的产品和供应商。现在我能够成功地为产品列表实现后台工作者,但是我无法在同一个后台线程中获得供应商列表。
它给了我调用异常。是否可以在单个后台线程中获取多个列表?
public void GetDetails()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerCompleted +=worker_RunWorkerCompleted;
IsBusy = true;
worker.RunWorkerAsync();
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ObservableCollection Products = (ObservableCollection)e.Result;
foreach (var prod in Products)
{
this.Products.Add(prod);
}
IsBusy = false;
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
ObservableCollection Products = new ObservableCollection();
ObservableCollection Supplier = new ObservableCollection();
using (Genesis_DataDataContext dataDc = new Genesis_DataDataContext())
{
foreach (tbl_Product thisProduct in dataDc.tbl_Products)
{
Products.Add(thisProduct);
}
//foreach (tbl_Supplier thisSupplier in dataDc.tbl_Suppliers)
//{
// Suppliers.Add(thisSupplier);
//}
}
e.Result = Products;
}
答案 0 :(得分:1)
我不知道您的 Genesis_DataDataContext()正在产生什么,但您可能只需创建一个基本类来加载数据:
class BasicClass {
public ObservableCollection Products { get; set; }
public ObservableCollection Suppliers { get; set; }
}
现在,有了这样的东西,你可以将两个数据项传回同一个对象。
public void GetDetails2()
{
using (var worker = new BackgroundWorker())
{
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += delegate (object s, DoWorkEventArgs e)
{
var w = (BackgroundWorker)s;
var bc = new BasicClass();
bc.Products = new ObservableCollection();
bc.Suppliers = new ObservableCollection();
using (var dataDc = new Genesis_DataDataContext())
{
// dbl_Products may use a Data Reader, so use an index.
// This may or may not produce an error, depending on how the Genesis_DataDataContext is designed.
for (int i = 0; i < dataDc.tbl_Products.Count; i++) {
w.ReportProgress(i + 1, dataDc.tbl_Products.Count);
var item = dataDc.tbl_Products[i];
bc.Products.Add(item);
}
w.ReportProgress(-1, "On to Suppliers.")
// dbl_Products may use a Data Reader, so use an index.
// This may or may not produce an error, depending on how the Genesis_DataDataContext is designed.
for (int i = 0; i < dataDc.tbl_Suppliers.Count; i++)
{
w.ReportProgress(i + 1, dataDc.tbl_Suppliers.Count);
var item = dataDc.tbl_Suppliers[i];
bc.Suppliers.Add(item);
}
e.Result = bc;
}
};
worker.ProgressChanged += delegate (object s, ProgressChangedEventArgs e)
{
if (!worker.CancellationPending)
{
var index = e.ProgressPercentage;
var state = e.UserState.ToString();
int total;
if (int.TryParse(state, out total))
{
Console.WriteLine("{0} of {1}", index, total);
} else
{
Console.WriteLine(state);
}
} else
{
worker.CancelAsync();
}
};
worker.RunWorkerCompleted += delegate (object s, RunWorkerCompletedEventArgs e)
{
IsBusy = false;
if (e.Error == null)
{
var bc = e.Result as BasicClass;
foreach (var item in bc.Products)
{
this.Products.Add(item);
}
foreach (var item in bc.Suppliers)
{
this.Suppliers.Add(item);
}
}
};
worker.RunWorkerAsync();
IsBusy = worker.IsBusy;
}
}
我认为 IsBusy 是您创建的另一个变量。我不确定它是什么。