I know this has been asked many times, but I tried all solutions I found and no one works for me.
I have the following code:
public async void setFile(QuickBrowseFile file) {
try {
LoggingDatabaseService db = new LoggingDatabaseService();
LoggingData data = await db.getLoggingData(file.Id);
db.close();
if (data != null) {
List<LoggingModel> loggingData = data.RowData;
int numRows = loggingData.Count;
Application.Current.Dispatcher.Invoke((Action)(delegate {
for (int i = 0; i < numRows; i++) {
LoggingModel m = loggingData[i];
treeView.Items.Add(m);//!!!!!!!!!!!!exception here
}
}));
}
} catch (Exception ex) {
Logger.error(ex);
}
}
I tried a different solution, but problem is that I have a gif image that shows some frames using the solution from How do I get an animated gif to work in WPF?.
Using the code below doesn't throw any exception, but block the gif image while loading..
var uiContext = TaskScheduler.FromCurrentSynchronizationContext();
await Task.Factory.StartNew(() => {
LoggingDatabaseService db = new LoggingDatabaseService();
//Console.WriteLine(file.Id);
LoggingData data = db.getLoggingData(file.Id);
db.close();
if (data != null) {
List<LoggingModel> loggingData = data.RowData;
int numRows = loggingData.Count;
for (int i = 0; i < numRows; i++) {
treeView.Items.Add(loggingData[i]);
}
}
}, CancellationToken.None, TaskCreationOptions.None, uiContext);