백그라운드 작업자를 실행하여 listbox를 mssql 데이터베이스의 값으로 업데이트하려고합니다. 나는이 나왔다 :자동 업데이트 C# 목록 상자
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
오류가 발생합니다.
어떻게 해결할 수 있습니까? 프로그램이 실행되는 동안 목록 상자에 대한 업데이트를 실행하고 싶습니다.
내에서 GUI 요소에 액세스 할 수
시도 상황의 종류에 들어가 당신은 여전히 LoadPrescriptions에서 예외가 발생하는지 확인하기 위해 – Adil
무엇? 내가 이해하지 못했기 때문에 더 설명 해주십시오. 나는 "for"LoadPrescription에 댓글을 달았지만 여전히 오류가 발생합니다. –
당신은 LoadPrescriptions 메서드에서 콤보처럼 일부 윈도우 컨트롤에 액세스 할 수 있습니까? – Adil