Unable to run the automatic task 'ConvertToTranslatableFormat' - file is locked or open in another process

I have an application which uses the automation APIs to create projects.

During the creation process the following steps take place:

  • Create a unique directory for this project
  • Download the source file(s) from our server and store them in \[unique dir]\original-files
  • Create another folder inside the unique directory, and whose name matches the name of the project

    So we now have

    \unique directory
         \ original-files
             \ [source file(s)]
         \ uniquely-named project directory

  • Create an instance of FileBasedProject
  • Add the files in \original-files to this new project object
    This is achieved by calling

        FileBasedProject.AddFolderWithFiles(origFilesDir.FullName, true);

  • Retrieve the language file ID(s) from the project
    This is achieved by calling

        var projectFiles = FileBasedProject.GetSourceLanguageFiles();
        var projectFileIds = projectFiles.GetIds();

  • Scan the language files
    This is achieved by calling

        FileBasedProject.RunAutomaticTask(projectFileIds, AutomaticTaskTemplateIds.Scan);

  • Convert the files to XLIFF
    And this is where I hit a problem: Failed to open the file '...'. The file is locked or open in another application.

Here is the code used for converting the file:


 

private static void ConvertFiles(IProject project, out Exception automaticTaskException)
{
	automaticTaskException = null;

	foreach (var sourceLangFile in project.GetSourceLanguageFiles().Where(slf => slf.Role == FileRole.Translatable))
	{
		Guid[] currentFileId = { sourceLangFile.Id };
		Exception e = null;

		Log.Info($"{nameof(ConvertFiles)}: Running automatic task to translate files to translatable format (i.e. SDL XLIFF).");
		project.RunAutomaticTask(currentFileId,
								 AutomaticTaskTemplateIds.ConvertToTranslatableFormat,
								 (sender, args) => Log.Info($"{nameof(ConvertFiles)}: Status: {args.StatusMessage} ({args.PercentComplete})"),
								 (sender, args) =>
								 {
									 if (args.Message.Exception != null)
									 {
										 e = args.Message.Exception;
									 }
									 Log.Info($"{nameof(ConvertFiles)}: Message: {args.Message}");
								 });

		if (e != null)
		{
			automaticTaskException = e;
			return;
		}
	}
}

As can be seen in the code, the conversion process returns messages which get logged:

ConvertFiles: Running automatic task to translate files to translatable format (i.e. SDL XLIFF).
ConvertFiles: Status: (0)|
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: [CORRECT PATH OF LANGUAGE FILE]
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: getting filter manager
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: getting converter
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: checking source language
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: checking target language
Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - ProcessFile: converting file
ConvertFiles: Status: (0)|

And then it throws an exception:

Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask| - Failed to open the file '[CORRECT PATH OF LANGUAGE FILE]'. The file is locked or open in another application.
at Sdl.FileTypeSupport.Framework.Adapter.Framework1.AbstractFramework1Adapter.ReThrowFilterFramework1Exception(Exception* x)
at Sdl.FileTypeSupport.Framework.Adapter.Framework1.Parser.ParseNext()
at Sdl.FileTypeSupport.Framework.Integration.FileExtractor.ParseNext()
at Sdl.FileTypeSupport.Framework.Integration.MultiFileConverter.ParseNext()
at Sdl.FileTypeSupport.Framework.Integration.MultiFileConverter.Parse()
at Sdl.ProjectApi.AutomaticTasks.Conversion.ConversionTask.ProcessFile(IExecutingTaskFile executingTaskFile)|

I'm not sure how the application can add the language file - and when doing so it makes its own copy of the file from the \original-files directory - and then find that the file is locked or open in another application. Surely the only application which has any awareness of this file is itself?