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