I'm really struggling to find a working solution to achieve an apparently easy task.
I open multiple layout projects to perform some operations in order to export a series of TIFF from them. I managed to work out an algorithm that allows to open the projects in sequence and perform the tasks I need. The code involves subscribing to MapViewInitializedEvent and LayoutViewEvent, where all the logic and the operations are actually performed, but the issue occurs on the main cycle, which is already quite small and simple, but I will provide here a further simplified version:
foreach (var project in ProjectList)
{
string currentProjectPath = Path.Combine(baseFolderPath, project.BasePath, project.FileName);
if (!File.Exists(currentProjectPath))
continue;
opRunning = true;
Project.Current.SetDirty(false);
await Project.OpenAsync(currentProjectPath);
while (opRunning)
Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, (DispatcherOperationCallback)delegate (object unused) { return null; }, null);
}
After I open the project with Project.OpenAsync, the events are fired and the TIFF are exported correctly. The while cycle is there to prevent the main foreach cycle to go on to the next project in the list before the exporting operations are finished. It does so by "waiting" on a background thread without causing the UI thread to lock. The opRunning flag is a static boolean, which is set to false at the end of the exporting process triggered by the events.
Everything seems to work just fine, except for the call to Project.Current.SetDirty(false): it just doesn't work and sometimes (seemingly at random) projects prompt the message window to ask for saving changes anyway.
I tried everything that came to my mind to prevent it from happening: subscribing to ProjectClosing o Closed events (calling SetDirty there) doesn't change anything, even saving the project before opening the new one is useless.
From what I can understand, something still sets the project to dirty state immediately before the new one is opened, but I really have no clue who is doing this and why. What I know for sure is that all the operations I perform on the project in order to export the TIFFs are already completed when the SetDirty(false) method is called, being this on the main cycle or via the ProjectClosingEvent.
I'm currently using 3.2.2 version of the Pro and, unless this is some kind of issue/bug with this version of the SDK, I must be missing something here: I'm pretty sure there must be a way to get rid of that request, so any suggestion would be really appreciated.
Thanks in advance