FernandoPaiva
FernandoPaiva

Reputation: 4460

BackgroundWork does not get value of ComboBox?

I have a ComboBox of objects and I do insert these values into my database, it work. Now I'm trying create a BackgroundWorker to insert and to control components of Form, for example buttons and progress and ComboBox itself. The problem is that after BackgroundWorker is added I can't get value selected in ComboBox and I can't understand what is this problem.

How could I solve it ?

Method to insert

/** 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);
                }
            }
        }

Button to start BackgroundWorker

private void btnSalvarPM_Click(object sender, EventArgs e) {    
    if (!backgroundWorker1.IsBusy) {
        progressBar1.Visible = true;                
        cbxPerfilModulo.Enabled = false;
        btnSalvarPM.Enabled = false;
        backgroundWorker1.RunWorkerAsync();
    }            
}

DoWork

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

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).

enter image description here

Upvotes: 0

Views: 613

Answers (1)

ChrisF
ChrisF

Reputation: 137148

You are trying to access the UI thread from the background worker. That's not allowed.

You'll need to pass the background worker all the information it needs to do it's business - in this case the selected item from the combo box.

In this case you'll have something like this:

  backgroundWorker1.RunWorkerAsync(cbxPerfilModulo.SelectedItem);

Then in the worker:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    Perfil perfil = (Perfil)e.Argument;
    insertPerfilModulo(perfil);
}

Where you will need to change your method to accept an argument of the item being processed.

Upvotes: 3

Related Questions