1

i want to remove entities on a background thread but we can not delete entities on another thread that it has be created on so how can i do that and keep the ui responding? i'm tying to use the backgroundworker class here is the code

void deletePeriodWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            Thread.Sleep(3000);
            List<Period> selectedPeriods = e.Argument as List<Period>;

            foreach (Period period in selectedPeriods)
            {
                while (period.Transactions.Count > 0)
                {
                    Transaction transaction = period.Transactions[0];
                    this.Dispatcher.Invoke(new Action(() => context.Transactions.Remove(transaction)), 
                        System.Windows.Threading.DispatcherPriority.Normal);
                }

                this.Dispatcher.Invoke(new Action(() => context.Periods.Remove(period)), 
                    System.Windows.Threading.DispatcherPriority.Normal);
            }
            this.Dispatcher.Invoke(new Action(() => context.SaveChanges()),
                    System.Windows.Threading.DispatcherPriority.Normal);
        }

1 Answer 1

2

Don't use 'foreach' to delete entities. When you delete a entity,the source is changed, it may throw an exception. Use 'for' instead. And why you delete it in a new thread?UI will be updated when all of the delete operations are finished.

try like this:

UI(List): itemsource={Binding LstTest}

background:

deletePeriodWorker_DoWork() {

        List<Period> selectedPeriods = e.Argument as List<Period>;

        foreach (Period period in selectedPeriods)
        {
            while (period.Transactions.Count > 0)
            {
                //operation
            }

            //ui updating
            this.Dispatcher.Invoke(new Action(() => LstTest.Remove(period)), 
                System.Windows.Threading.DispatcherPriority.Normal);

            //EF updating
            context.Periods.Remove(period);

        }

        //context savechanges

}

Sign up to request clarification or add additional context in comments.

6 Comments

Seeing that the source is a plain List<T> it can't be changed by deleting entities, so using foreach is perfectly fine and more readable.
Rang but what will happen while the entities are being deleted if it takes a while will the ui remain responding?
Yes you are right.I am not seeing that the code is context.Periods.Remove(XX). We can't add/remove items to the collection in foreach.
Hi rang let say i'm deleting 1000 transactions it could take 2/3 seconds so what will be than?
Motty, Try another way.Don't creat relationship between delete entities and UI updating. UI's itemsource is binding with a list,add/remove it's items in a new thread.Don't delete entites in a new thread.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.