C# WPF Desktop App - List - Threading problem - locking problem

Markus Freitag 3,791 Reputation points
2023-01-14T08:49:06.8533333+00:00

Hello,

Concecpt_05

I have a user interface where I fill a list.
At the same time, another process can update or delete the items.
It crashes when I add new items at the same time the process is deleting an item because it is processed. So it's finished.

The questions now, how can I make sure that when I add, the deletion of items is then delayed. How can I lock it?

Do you have sample code for me? Possible solutions? Thank you for your help in advance.
I use VS2019, C#, NET 4.72, Win10, Winforms or WPF Desktop App

Developer technologies | Windows Presentation Foundation
Developer technologies | C#
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2023-01-14T16:36:20.8733333+00:00

    The UI controls are not thread safe and must be updated from the UI thread. The UI thread should be the only thread accessing the list control. For the background thread to call the ui thread use dispatcher

    [https://learn.microsoft.com/en-us/dotnet/api/system.windows.threading.dispatcher.invoke?view=windowsdesktop-7.0


2 additional answers

Sort by: Most helpful
  1. محمود اليازجي 0 Reputation points
    2023-01-15T19:48:59.3633333+00:00
    public void ListProdcess(int mode)
    {
    	lock( ListProcess.SyncRoot )
    	{
    		  switch (mode)
    		  {
    		  default:
    			 Exception
    			  break;
    		  case 1: // fill
    			 break;
    		  case 2: // modify
    			 break;
    		   case 3: // delete
    			 break;
    		  }
    	}
    }
    
    0 comments No comments

  2. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2023-01-19T08:20:11.85+00:00

    You could try to use bindingOperations.enableCollectionSyNChronization Method .

    The documentation is pretty clear on this:

    To use a collection on multiple threads, one of which is the UI thread that owns the ItemsControl, an application has the following responsibilities:

    • Choose a synchronization mechanism.
    • Synchronize all access from the application to the collection using that mechanism.
    • Call EnableCollectionSynchronization to inform WPF of the mechanism.
    • ... You can lock the collection when modifying it from another thread.

    On dispatcher (UI) thread:

    _itemsLock = new object();
    Items = new ObservableCollection<Item>();
    BindingOperations.EnableCollectionSynchronization(Items, _itemsLock);
    

    Then from another thread:

    lock (_itemsLock)
    {
    
    // Once locked, you can manipulate the collection safely from another thread
    Items.Add(new Item());
    }
    
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.