From 9d7b6fbfd44d212c62d2d294af14d41178bff78e Mon Sep 17 00:00:00 2001 From: Baudin999 Date: Thu, 26 Dec 2019 12:12:47 +0100 Subject: [PATCH] Added awaiting to the DeleteModule, lets see if the tests succeede --- Project/FileProject.Watch.cs | 6 +-- Project/FileProject.cs | 84 ++++++++++++++++++------------------ Project/IO.cs | 10 +++++ Project/Module.cs | 5 ++- 4 files changed, 58 insertions(+), 47 deletions(-) diff --git a/Project/FileProject.Watch.cs b/Project/FileProject.Watch.cs index d61a0ea..39a5ca0 100644 --- a/Project/FileProject.Watch.cs +++ b/Project/FileProject.Watch.cs @@ -39,13 +39,13 @@ public void Watch() // } //}); - ProjectWatcher.ModuleStream.Subscribe("OnDelete", MessageType.ModuleDeleted, msm => + ProjectWatcher.ModuleStream.Subscribe("OnDelete", MessageType.ModuleDeleted, async msm => { var module = Modules.FirstOrDefault(m => m.Name == msm.ModuleName); if (!(module is null)) { - module.Clean(); Modules.Remove(module); + await module.Clean(); } }); @@ -54,7 +54,7 @@ public void Watch() var moduleOld = Modules.FirstOrDefault(m => m.Name == msm.ModuleName); if (!(moduleOld is null)) { - moduleOld.Clean(); + await moduleOld.Clean(); Modules.Remove(moduleOld); } var module = new Module(msm.FileFullPath, this.BasePath, this); diff --git a/Project/FileProject.cs b/Project/FileProject.cs index 1d25ed9..ea910bc 100644 --- a/Project/FileProject.cs +++ b/Project/FileProject.cs @@ -128,35 +128,35 @@ public List GetAstForModule(string moduleName) return this.Modules.FirstOrDefault(m => m.Name == name); } - public Task DeleteModule(string moduleName) + public async Task DeleteModule(string moduleName) { var tcs = new TaskCompletionSource(); try { var module = FindModule(moduleName); - if (module is null) return Task.FromResult(false); + if (module is null) return await Task.FromResult(false); if (this.ProjectWatcher != null) { - Task.Factory.StartNew(() => - { - Action cleanup = () => { }; - var listenerName = "CREATEOR: " + DateTime.Now + new Random().Next(1000000).ToString(); - cleanup = this.ProjectWatcher.ModuleStream.Subscribe(listenerName, MessageType.ModuleDeleted, msm => - { - tcs.TrySetResult(true); - Task.Run(cleanup); - }); - }); + await Task.Factory.StartNew(() => + { + Action cleanup = () => { }; + var listenerName = "CREATEOR: " + DateTime.Now + new Random().Next(1000000).ToString(); + cleanup = this.ProjectWatcher.ModuleStream.Subscribe(listenerName, MessageType.ModuleDeleted, msm => + { + tcs.TrySetResult(true); + Task.Run(cleanup); + }); + }); } - File.Delete(module.FilePath); + await IO.DeleteFile(module.FilePath); } catch (Exception ex) { tcs.TrySetException(ex); } - return tcs.Task; + return await tcs.Task; } public void Dispose() @@ -170,34 +170,34 @@ public void Dispose() } } -// private void CreateNewModuleFile(string modulePath, string moduleName, string? code = null) -// { -// try -// { -// using (var fs = new FileStream(modulePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)) -// { -// using (var sr = new StreamWriter(fs)) -// { -// sr.Write(code ?? $@" -//# {moduleName} - -//Have fun with your module! -//"); -// sr.Flush(); -// sr.Close(); -// sr.Dispose(); -// } -// fs.Close(); -// fs.Dispose(); - -// } -// } -// catch (IOException ioe) -// { -// Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); -// throw ioe; -// } -// } + // private void CreateNewModuleFile(string modulePath, string moduleName, string? code = null) + // { + // try + // { + // using (var fs = new FileStream(modulePath, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite)) + // { + // using (var sr = new StreamWriter(fs)) + // { + // sr.Write(code ?? $@" + //# {moduleName} + + //Have fun with your module! + //"); + // sr.Flush(); + // sr.Close(); + // sr.Dispose(); + // } + // fs.Close(); + // fs.Dispose(); + + // } + // } + // catch (IOException ioe) + // { + // Console.WriteLine("ReadModuleText: Caught Exception reading file [{0}]", ioe); + // throw ioe; + // } + // } static string UppercaseFirst(string s) diff --git a/Project/IO.cs b/Project/IO.cs index 1ac939d..8edb7a6 100644 --- a/Project/IO.cs +++ b/Project/IO.cs @@ -65,5 +65,15 @@ internal static async Task SaveFile(string fileName, string directoryName, strin throw ioe; } } + + internal static async Task DeleteFile(string fileName) + { + await Task.Run(() => File.Delete(fileName)); + } + + internal static async Task DeleteDirectory(string directoryName) + { + await Task.Run(() => Directory.Delete(directoryName, true)); + } } } diff --git a/Project/Module.cs b/Project/Module.cs index 2e46b1f..165102c 100644 --- a/Project/Module.cs +++ b/Project/Module.cs @@ -116,13 +116,14 @@ public async Task SaveModuleOutput(bool decend = true, bool suppressMessage = fa }); } } - public void Clean() + public async Task Clean() { try { if (OutPath != null && Directory.Exists(OutPath)) { - Task.Factory.StartNew(() => Directory.Delete(OutPath, true)); + //Directory.Delete(OutPath, true); + await Task.Run(() => Directory.Delete(OutPath, true)); } } catch (Exception ex)