Select to view content in your preferred language

How to (really) prevent project from prompting for saving changes

186
1
Jump to solution
a month ago
Asimov
by
New Contributor III

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

1 Solution

Accepted Solutions
Asimov
by
New Contributor III

Update: I changed the approach of cycling through the projects and got rid of the while cycle that "sleeps" on the background thread. In order to switch from a project to the next in a cleaner way, I managed to build a solution based on a custom event that my code fires after every job is finished. The event is listened from the caller, which in turn can go on and load the next project.

With this approach everything works in a much cleaner and smooth way, but initially I still had the issue about the SetDirty(false) call. I managed to get rid of it introducing a simple Task.Sleep(500) call in the event handler (the one called after a single job is finished when my custom event is fired), before calling my engine for the next job (that in turn opens the new project and repeats the cycle). That finally took away the request to save the project, don't really know whether this is by design, but I doubt it. 

I also observed odd behaviors with SetDirty(false) call in other (much simpler) contexts, and that leads me to think that maybe there could be something broken inside the SDK around it. 

In any case that solved my issue: can't share the code about this because it is quite a lot, but hopefully the description is enough to help someone who will bump into the same problem.

View solution in original post

0 Kudos
1 Reply
Asimov
by
New Contributor III

Update: I changed the approach of cycling through the projects and got rid of the while cycle that "sleeps" on the background thread. In order to switch from a project to the next in a cleaner way, I managed to build a solution based on a custom event that my code fires after every job is finished. The event is listened from the caller, which in turn can go on and load the next project.

With this approach everything works in a much cleaner and smooth way, but initially I still had the issue about the SetDirty(false) call. I managed to get rid of it introducing a simple Task.Sleep(500) call in the event handler (the one called after a single job is finished when my custom event is fired), before calling my engine for the next job (that in turn opens the new project and repeats the cycle). That finally took away the request to save the project, don't really know whether this is by design, but I doubt it. 

I also observed odd behaviors with SetDirty(false) call in other (much simpler) contexts, and that leads me to think that maybe there could be something broken inside the SDK around it. 

In any case that solved my issue: can't share the code about this because it is quite a lot, but hopefully the description is enough to help someone who will bump into the same problem.

0 Kudos