I am a wpf newbie so pardon anything that doesn’t sound right…
I have a WPF application that when started loads many (20000+) items from my SQL database and stores them into an ObservableCollection which is created in the xaml.cs behind file.
I created a class whose constructor creates a thread and starts the thread and the thread loops infinitely.
public StatusChanged()
{
Thread thread = new Thread(new ThreadStart(ThreadProc));
thread.Start();
}
The way this thread gets started is by creating an instance of this thread class in my main window behind code (xaml.cs)
In this thread I have a while loop that does a few things that may be causing my UI window to become unresponsive:
-
infinite while loop that runs a sql command checking to see if a bit flag has changed for any of the items in the database. if none are found, the thread sleeps 3 seconds and the loop continues
-
when rows are returned from the database, I loop through every row and find the changed attribute and modify my ObservableCollection as well as modifying the bit flag in the database by running a SQL Update command for each one. The thread then sleeps for 3 seconds again and the loop continues.
This is an overview of what is occurring. When rows are returned with from the database and the work is being done from point 2), the UI can become unresponsive. When I try to click a button it normally works say 75%+ of the time, but 25% of the time the UI is unresponsive and nothing happens. This is very bad for my app.
Based on my example, what do you think is making the UI unresponsive? could it be…
-
the while loop that is looping through every row returned by the database?
-
Updating the observable collection inside of that while loop ^^ as well as making a sql update call for each item.
-
The way my thread is started and sleeps and created by an instance from the xaml.cs code?
If the Observable collection of 20000+ items is bound to the UI, the UI might need to be updated when the items are modified. This can be very expensive for the UI thread causing the UI to become unresponsive.
A couple of suggestions to try: