Backgroundworker does not receive selected Combobox value

Asked

Viewed 105 times

1

I have an object Combobox and I am creating a Backgroundworker to add these objects to my database. Without using the Backgroundworker I can catch the selected object but when I use the Backgroundworker returns an exception and the object is empty.

How to solve this ?

I’m trying like this.

Method that the 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 that runs

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).
  • Has attempted to specify the members in which the backgroundWolker1 will you access? For example, put nomeDaClasse.progressBar1.Visible = true;

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 in a Form control by the thread in which the Form 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 a control.

Source: Control.Invokerequired

Since you are running the code on a Background Worker (i.e., another thread), the exception is generated.

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

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    Invoke((MethodInvoker) insertPerfilModulo());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.