Alfonsus Dhani
Alfonsus Dhani

Reputation: 105

“Cross-thread operation is not valid” exception when trying to get value of comboBox in Backgroundworker

I have some problem when trying to get value of combobox in IF statement in Backgroundworker. When I try to run this code

 if (KondisiSaldo.SelectedItem == "Kurang dari...")
 {
   view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
   view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);


   this.Invoke(new MethodInvoker(delegate
   {
     ViewDataSaldoGV.DataSource = view;
     SaldoUnscheduleGV.DataSource = view2;
   }));
}

The error says

Cross-thread operation not valid: Control 'KondisiSaldo' accessed from a thread other than the thread it was created on.

Can anyone help me ?

Upvotes: 1

Views: 75

Answers (1)

Adil
Adil

Reputation: 148150

You are accessesing KondisiSaldo in non GUI thread. Put the KondisiSaldo in Invoke block to access it on GUI thread as your did with view and view2 controls.

this.Invoke(new MethodInvoker(delegate
{
   if (KondisiSaldo.SelectedItem == "Kurang dari...")
   {
         view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
         view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);   

         ViewDataSaldoGV.DataSource = view;
         SaldoUnscheduleGV.DataSource = view2;
   }
}));

You may need to adjust the condition you have.

Upvotes: 4

Related Questions