我想运行一个后台工作程序来使用 mssql 数据库中的值更新列表框。我出来了:
public frmMain() {
InitializeComponent();
bw.DoWork += new DoWorkEventHandler(bw_DoWork);
}
private void frmMain_Load(object sender, EventArgs e) {
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e){
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; (i <= 10); i++) {
if ((worker.CancellationPending == true)) {
e.Cancel = true;
break;
}
else {
(1) LoadPrescriptions(); //load the date in a list and writes the list into the listbox
(2) System.Threading.Thread.Sleep(500);
}
}
}
private void LoadPrescriptions()
{
main_controller = new MainController();
prescriptionsList = new List<Prescription>();
prescriptionsList = main_controller.LoadPrescriptions(0);
lstPrescriptions.Items.Clear();
for (int i = 0; i < prescriptionsList.Count; i++)
lstPrescriptions.Items.Add(prescriptionsList[i].name + " " + prescriptionsList[i].surname);
}
在(1)和(2)之间的某个地方我得到了A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
错误。
关于如何解决这个问题的想法?只要程序正在运行,我只想运行列表框的更新。