Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
自动更新C#列表框_C#_Winforms_Backgroundworker_Auto Update - Fatal编程技术网

自动更新C#列表框

自动更新C#列表框,c#,winforms,backgroundworker,auto-update,C#,Winforms,Backgroundworker,Auto Update,我想运行后台工作程序,用mssql数据库中的值更新列表框。我说的是: public frmMain() { InitializeComponent(); bw.DoWork += new DoWorkEventHandler(bw_DoWork); } private void frmMain_Load(object sender, EventArgs e) { if (bw.

我想运行后台工作程序,用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);
    }
public frmMain(){
初始化组件();
bw.DoWork+=新DoWorkEventHandler(bw_DoWork);
}
私有void frmMain_加载(对象发送方,事件参数e){
如果(bw.IsBusy!=真)
{
RunWorkerAsync();
}
}
私有void bw_DoWork(对象发送方,DoWorkEventArgs e){
BackgroundWorker worker=发件人作为BackgroundWorker;

对于(inti=1;(i当我们从另一个GUI线程访问一些GUI控件时,我们就进入了这种情况

尝试访问此委托结构中的GUI元素

        MethodInvoker objMethodInvoker = delegate
        {
             //access and assign data to list control here               
        };
        if (InvokeRequired)
            BeginInvoke(objMethodInvoker);
        else
            objMethodInvoker.Invoke();

不要访问GUI元素并进行调试,以查看是否仍然在LoadPrescriptionswhat中出现异常?请更好地解释,因为我不明白。我对“for”进行了注释在LoadPrescriptions中,但我仍然得到错误。我的意思是,您是否在LoadPrescriptions方法中访问某些Windows控件,如combo?是的。我将加载的数据写入列表框。更新工作正常。现在我必须处理索引超出范围的异常。检查您是否正在访问某些集合,如超出其大小的数组,如访问第四个数组数组中的元素大小为三个元素我想程序中有错误,因为我再也不会出现这个错误了。非常感谢你的回答。