I'm creating a FileSystemWatcher with WPF MVVM pattern. When watcher is started it gets the oldest *.json file from the Input folder and the file gets copied to the Output folder I activate a external program `Autodesk Inventor` that reads the *.json file and makes the 3D model and 2D drawings and so on.
I have 2 ObservableCollections
How can I make this that the UI makes the changes and shows the changes in the collection right away?
How it works, start debugging, set the input and output folder (i created a input and output folder in the debug folder). When in debugmode the solution checks for simple .txt files (these are in the inputfolder) press START in the right corner to start the watcher it will move the oldest file to the output folder start the process (in debugmode Thread.Sleep(15000)). In debugmode copy the Example1_OK.txt in the inputfolder and the process will start again. My issue is that the UI does not update untill the process is done wich is very confusing for the end user.
I tried using backgroundworkers and Dispatcher but no results
Thanks in advance
Sandhiya PriyaPosted Jan 2, 2026, 11:02 AM
This is a classic UI thread blocking issue in WPF. Your
ObservableCollectionitself is fine — WPF DataGrid will update immediately when you add/remove items. The reason you only see changes afterStartProcess()finishes is because you’re running long-running work (likeThread.Sleep(15000)or launching Inventor) on the UI thread, which prevents the UI from repainting until the method returns.Why It Happens
WPF UI runs on a single Dispatcher thread.
If you block that thread with
Thread.Sleep, external process calls, or heavy loops, the UI cannot process rendering or collection change notifications.The
ObservableCollectionraisesCollectionChangedevents immediately, but the UI can’t handle them until the thread is free again.How to Fix It
You need to move the long-running work off the UI thread. Options:
1. Use
async/awaitwithTask.RunBecause you
await Task.Run, the UI thread is free to update the DataGrid while the background task runs.2. Use
Dispatcher.Invokeonly for UI updatesIf you’re inside a background thread and need to update the collection:
But don’t wrap the whole process in
Dispatcher.Invoke— only the UI updates.3. Avoid
Thread.SleepReplace it with
await Task.Delay(15000)inside an async method. That way the UI thread isn’t blocked.Practice for MVVM
Keep your collections (
ObservableCollection) in your ViewModel.Expose async commands (e.g., using
ICommandwithasync Task Execute).Do heavy work in background tasks, update collections on the UI thread.
Never block the UI thread with
Thread.Sleepor synchronous external calls.Example Command
Note
Your DataGrid is updating correctly — but the UI thread is blocked.
Move your long-running work to a background task (
Task.Run,async/await) and only marshal back to the UI thread for collection updates.