MattNorman
Well-known member
- Joined
- May 22, 2021
- Messages
- 98
- Programming Experience
- 1-3
I have a collection in my ViewModel which is:
I am calling a method in a static class that takes this collection as a parameter and then uses ClosedXML to write data to a spreadsheet.
Originally I was running the export method without forcing it to run on the UI thread and it was all fine.
The view for this report has a toggle which turns the date field on/off and updates the collection.
I found that after toggling the dates on, the export method was throwing an exception (The calling method cannot access the object as it is owned by another thread).
I then changed the export method to use Application.Current.Dispatcher.Invoke to run it on the UI thread.
This resolved the issue exporting data when the ate toggle had been turned on. It however then broke the export when the toggle was not switched on.
I'm not sure how a toggle that simply reloads the collection data and unhides a data grid column can cause this issue. The collection is modified and loaded with data before the export function is called in any case.
The export methods fails at this step which is using the ClosedXML InsertTable method to write the collection to the worksheet.
It's not clear if the exception definitely relates to the collection that is being passed or if it's the worksheet object itself.
In either case, the worksheet object is currently being created on the UI thread along with the rest of the export method.
I have also tried making a new copy of the collection that is not bound to any controls and passing that however it did not make any diffrence.
C#:
public ObservableCollection<ReportAgentCallSummaryModel> ReportData { get; set; } = new ObservableCollection<ReportAgentCallSummaryModel>();
I am calling a method in a static class that takes this collection as a parameter and then uses ClosedXML to write data to a spreadsheet.
Originally I was running the export method without forcing it to run on the UI thread and it was all fine.
The view for this report has a toggle which turns the date field on/off and updates the collection.
I found that after toggling the dates on, the export method was throwing an exception (The calling method cannot access the object as it is owned by another thread).
I then changed the export method to use Application.Current.Dispatcher.Invoke to run it on the UI thread.
This resolved the issue exporting data when the ate toggle had been turned on. It however then broke the export when the toggle was not switched on.
I'm not sure how a toggle that simply reloads the collection data and unhides a data grid column can cause this issue. The collection is modified and loaded with data before the export function is called in any case.
The export methods fails at this step which is using the ClosedXML InsertTable method to write the collection to the worksheet.
C#:
var table = worksheet.Cell("B5").InsertTable(reportData);
It's not clear if the exception definitely relates to the collection that is being passed or if it's the worksheet object itself.
In either case, the worksheet object is currently being created on the UI thread along with the rest of the export method.
I have also tried making a new copy of the collection that is not bound to any controls and passing that however it did not make any diffrence.