BackgroundWorker does not get selected value from ComboBox

1

I have a ComboBox of objects and I'm creating a BackgroundWorker to add these objects to my database. Without using the BackgroundWorker I can get the selected object but when I use the BackgroundWorker it returns an exception and the object is empty.

How to solve this?

I'm trying like this.

Method BackgroundWorker performs

/** insere Perfil + Modulo */
        private void insertPerfilModulo() {            
            Perfil perfil = (Perfil)cbxPerfilModulo.SelectedItem;
            IList<Modulo> lista = getListaModulo();

            foreach(Modulo m in lista){                
                Permissao permissao = new Permissao();
                permissao.perfil = perfil;
                permissao.modulo = m;

                Boolean exist = dao.isExistPerfilAndModulo(permissao);                
                if (exist) {
                    Permissao p = dao.getPermissao(permissao);                    
                    dao.update(p);
                }else {
                    dao.insert(permissao);
                }
            }
        }

DoWork

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
            progressBar1.Visible = true;
            insertPerfilModulo();
        }

Boot executing

private void btnSalvarPM_Click(object sender, EventArgs e) {            
            backgroundWorker1.RunWorkerAsync();
        }

Exception

The thread '<No Name>' (0x1870) has exited with code 0 (0x0).
'PubControl.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Numerics\v4.0_4.0.0.0__b77a5c561934e089\System.Numerics.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
A first chance exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
The program '[5396] PubControl.vshost.exe: Program Trace' has exited with code 0 (0x0).
The program '[5396] PubControl.vshost.exe: Managed (v4.0.30319)' has exited with code -1 (0xffffffff).
    
asked by anonymous 11.03.2016 / 20:46

1 answer

1

Most likely the "System.InvalidOperationException" exception refers to "Cross-thread operation not valid".

This is because it is only possible to make changes to a control of a Form by the thread in which its window is located.

  

Controls in Windows Forms are bound to a specific thread and are not   thread safe. Therefore, if you are calling a control's method from a   different thread, you must use one of the control's invoke methods to   marshal the call to the proper thread. This property can be used to   determine if you must call an invoke method, which can be useful if   you do not know what thread owns to control.

Source: Control.InvokeRequired

Since you are running the code in a Background Worker (ie another thread), the exception is thrown.

The solution is to use an invoke to call the insertPerfilModulo() method and move progressBar1.Visible = true; to it.

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    Invoke((MethodInvoker) insertPerfilModulo());
}
    
12.03.2016 / 21:09