Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DYN-7436] Dynamo crashes upon exporting results if the destination file is open #66

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 66 additions & 25 deletions TuneUp/TuneUpWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1187,54 +1187,95 @@ public void ExportToCsv()

if (saveFileDialog.ShowDialog() == true)
{
using (var writer = new StreamWriter(saveFileDialog.FileName))
// Check if the .csv file locked or in use
if (IsFileLocked(new FileInfo(saveFileDialog.FileName)))
{
writer.WriteLine("Execution Order,Name,Execution Time (ms)");
MessageBox.Show("The file is currently in use by another application. Please close the file before trying to overwrite it.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ivaylo-matov can you move these messages to resources so that they will be localized. You can do it in a different PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now addressed in this PR : #70

"File in Use", MessageBoxButton.OK, MessageBoxImage.Warning);
return;
}

var collections = new (string Label, CollectionViewSource Collection, string TotalTime)[]
try
{
using (var writer = new StreamWriter(saveFileDialog.FileName))
{
writer.WriteLine("Execution Order,Name,Execution Time (ms)");

var collections = new (string Label, CollectionViewSource Collection, string TotalTime)[]
{
("Latest Run", ProfiledNodesCollectionLatestRun, LatestGraphExecutionTime),
("Previous Run", ProfiledNodesCollectionPreviousRun, PreviousGraphExecutionTime),
("Not Executed", ProfiledNodesCollectionNotExecuted, null)
};
};

foreach (var (label, collection, totalTime) in collections)
{
var nodes = collection.View.Cast<ProfiledNodeViewModel>().ToList();
if (!nodes.Any()) continue;
foreach (var (label, collection, totalTime) in collections)
{
var nodes = collection.View.Cast<ProfiledNodeViewModel>().ToList();
if (!nodes.Any()) continue;

writer.WriteLine(label);
writer.WriteLine(label);

foreach (var node in nodes)
{
if (showGroups)
foreach (var node in nodes)
{
if (node.IsGroup || node.GroupGUID == Guid.Empty)
if (showGroups)
{
writer.WriteLine($"{node.GroupExecutionOrderNumber},{node.Name},{node.ExecutionMilliseconds}");
if (node.IsGroup || node.GroupGUID == Guid.Empty)
{
writer.WriteLine($"{node.GroupExecutionOrderNumber},{node.Name},{node.ExecutionMilliseconds}");
}
else
{
writer.WriteLine($",{node.Name},{node.ExecutionMilliseconds}");
}
}
else
else if (!node.IsGroup || !node.IsGroupExecutionTime)
{
writer.WriteLine($",{node.Name},{node.ExecutionMilliseconds}");
writer.WriteLine($"{node.ExecutionOrderNumber},{node.Name},{node.ExecutionMilliseconds}");
}
}
else if (!node.IsGroup || !node.IsGroupExecutionTime)

// Write total execution time, if applicable
if (!string.IsNullOrEmpty(totalTime))
{
writer.WriteLine($"{node.ExecutionOrderNumber},{node.Name},{node.ExecutionMilliseconds}");
writer.WriteLine($",Total, {totalTime}");
}
writer.WriteLine();
}

// Write total execution time, if applicable
if (!string.IsNullOrEmpty(totalTime))
{
writer.WriteLine($",Total, {totalTime}");
}
writer.WriteLine();
}
}
catch (IOException ex)
{
MessageBox.Show($"An error occurred while trying to write the file: {ex.Message}",
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}

/// <summary>
/// Checks if the specified file is locked by another process or application.
/// </summary>
private bool IsFileLocked(FileInfo file)
{
if (!file.Exists) return false;

FileStream stream = null;

try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
return true;
}
finally
{
stream?.Close();
}

return false;
}

/// <summary>
/// Exports the ProfiledNodesCollections to a JSON file.
/// </summary>
Expand Down