Skip to content

Commit

Permalink
refactor: Fixed PackageService.SavePlcLibraryAsync
Browse files Browse the repository at this point in the history
The use of ThreadLocal with `AutomationInterface` is not good.
  • Loading branch information
ahuca committed Jan 1, 2024
1 parent 0eccba6 commit c3aa0cc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 10 deletions.
10 changes: 3 additions & 7 deletions src/TwinGet.Core/Packaging/PackageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,11 @@ private static async Task<string> SavePlcLibraryAsync(IPackCommand packCommand)

StaTaskScheduler staTaskScheduler = new(1);

using var ai = new ThreadLocal<AutomationInterface>(() => new AutomationInterface());

// Begin AutomationInterface.
var initAiTask = Task.Factory.StartNew(
() =>
{
var _ = ai.Value; // We do this so that ThreadLocal inititalize AutomationInterface();
return new AutomationInterface();
},
CancellationToken.None,
TaskCreationOptions.None,
Expand All @@ -195,21 +193,19 @@ private static async Task<string> SavePlcLibraryAsync(IPackCommand packCommand)
var savePlcLibTask = initAiTask.ContinueWith(
(prevTask) =>
{
prevTask.Wait();
using AutomationInterface ai = prevTask.Result;
lock (libraryPathLock)
{
try
{
// Suppress because we are in try block.
#pragma warning disable CS8602 // Dereference of a possibly null reference.
libraryPath =
ai.Value.SavePlcProject(
ai.SavePlcProject(
packCommand.Path,
packCommand.OutputDirectory,
resolvedSolution
) ?? string.Empty;
#pragma warning restore CS8602 // Dereference of a possibly null reference.
}
catch (PackagingException ex)
{
Expand Down
4 changes: 2 additions & 2 deletions src/TwinGet.TwincatInterface/TwincatProject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public TwincatProject(EnvDTE.Project project)
_plcProjects = new(TryGetPlcProjects(_systemManager, _project.FullName));
}

private static IReadOnlyList<PlcProject> TryGetPlcProjects(
private static List<PlcProject> TryGetPlcProjects(
ITcSysManagerAlias systemManager,
string filePath
)
{
List<PlcProject> plcProjects = new();
List<PlcProject> plcProjects = [];

ArgumentException.ThrowIfNullOrEmpty(filePath, nameof(filePath));

Expand Down
2 changes: 1 addition & 1 deletion test/Test.Utils.Test/TestTwincatProjectTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This file is licensed to you under MIT license.
using FluentAssertions;

namespace Test.Utils;
namespace Test.Utils.Test;

public class TestTwincatProjectTests
{
Expand Down

0 comments on commit c3aa0cc

Please sign in to comment.