Completed
Last Updated: 11 Oct 2018 14:47 by Dimitar
ADMIN
Dess | Tech Support Engineer, Principal
Created on: 05 Oct 2018 06:54
Category:
Type: Bug Report
1
FIX. RadDropDownList - InvalidOperationException ("Cross-thread operation not valid") when adding a RadDropDownList to the Form.Controls collection in the BackgroundWorker.RunWorkerCompleted event
To reproduce: run the attached sample project.

        private List<String> dropdownValues = new List<string> {"123", "abc", "!@#"};
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        { 
            radDropDownList1.DataSource = dropdownValues;
            RadButton b = null;
            RadDropDownList ddl = null;
            using (var bg = new BackgroundWorker())
            {
                bg.DoWork += (s, e1) =>
                {
                    Type t =typeof(RadDropDownList);
 
                    ddl = (RadDropDownList) Activator.CreateInstance(t);
                    ddl.DataSource = dropdownValues.OrderBy(l=>l);

                };
                bg.RunWorkerCompleted += (s, e1) =>
                {
                   
                        this.BeginInvoke(new Action(() =>
                        {
                            //this.Controls.Add(b);
                            this.Controls.Add(ddl);
                         
                        }));
                };
                bg.RunWorkerAsync();
            }


        }

Workaround: initialize the RadDropDownLIst outside the DoWork event handler.
0 comments