From 5dcbbf93ff01031f7f8bb544b2960fc3334b7ec2 Mon Sep 17 00:00:00 2001 From: Jonathon <34756063+spliffli@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:25:03 +0100 Subject: [PATCH 1/5] Update Helpers.cs Updated the tryReadFile method so that it handles some exceptions and I ran into all 3 of these exceptions while building upon this code in another project and thought it might be worth contributing. Because the tryReadFile method frequently gets called from a loop for checking for a file at a given path, returning an empty string after handling each exception means it will just try again at the next loop and operate smoothly without crashing the program. - DirectoryNotFoundException can happen when the MetaTrader EA either isn't running or isn't initialized properly, and it returns an empty string to continue the loop that's constantly checking a filepath until it either works or possibly it can be updated to infer the status of the EA as offline which could be useful. - FileNotFoundException was happening in my program even when the EA was running, and I found that by creating an empty file at location where it's expected to be fixes the problem, so it does that and returns an empty string. - IOException happens when more than one process accesses the same resource at the exact same time a.k.a. a race condition. This can be handled by just returning an empty string. --- dotnet/DWXConnect/api/Helpers.cs | 56 ++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/dotnet/DWXConnect/api/Helpers.cs b/dotnet/DWXConnect/api/Helpers.cs index eefd27e..17bc993 100644 --- a/dotnet/DWXConnect/api/Helpers.cs +++ b/dotnet/DWXConnect/api/Helpers.cs @@ -65,27 +65,47 @@ public static void tryDeleteFile(string path) } - /*Formats a double value to string. - - Args: - value (double): numeric value to format. - - */ - public static string format(double value) + /*Formats a double value to string. + + Args: + value (double): numeric value to format. + + */ + public static string format(double value) { return value.ToString("G", CultureInfo.CreateSpecificCulture("en-US")); } - public static string tryReadFile(string path) - { - try - { - return File.ReadAllText(path); - } - catch - { - return ""; - } - } + /*Tries to read a file and handles various exceptions which then return an empty string and writes the exception to the console. + + */ + public static string tryReadFile(string path) + { + try + { + return File.ReadAllText(path); + } + catch (DirectoryNotFoundException) + { + Console.WriteLine("api.Helpers.tryReadFile | DirectoryNotFoundException. Returning empty string."); + return ""; + } + catch (FileNotFoundException) + { + Console.WriteLine($"api.Helpers.tryReadFile | FileNotFoundException. Creating empty file at path ({path}) & returning empty string."); + CreateEmptyFile(path); + return ""; + } + catch (IOException) + { + Console.WriteLine("api.Helpers.tryReadFile | IOException. Race condition. Most likely this process and the MetaTrader EA both trying to access/use the file simultaneously. Returning empty string."); + return ""; + } + } + + public static void CreateEmptyFile(string filepath) + { + File.Create(filepath).Dispose(); + } } } From a579ae2471bf781a8c15ed458aa2458d35f1011f Mon Sep 17 00:00:00 2001 From: Jonathon <34756063+spliffli@users.noreply.github.com> Date: Tue, 26 Mar 2024 22:40:56 +0100 Subject: [PATCH 2/5] Update Helpers.cs Changed CreateEmptyFile method from public to private --- dotnet/DWXConnect/api/Helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/DWXConnect/api/Helpers.cs b/dotnet/DWXConnect/api/Helpers.cs index 17bc993..aefe61b 100644 --- a/dotnet/DWXConnect/api/Helpers.cs +++ b/dotnet/DWXConnect/api/Helpers.cs @@ -103,7 +103,7 @@ public static string tryReadFile(string path) } } - public static void CreateEmptyFile(string filepath) + private static void CreateEmptyFile(string filepath) { File.Create(filepath).Dispose(); } From 4f0bd7a17b1dbd8109ad0c78804f306d28dd9bf2 Mon Sep 17 00:00:00 2001 From: Jonathon <34756063+spliffli@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:22:15 +0100 Subject: [PATCH 3/5] Update Helpers.cs Added a private method GetFileNameFromPath which returns the filename at the end of a filepath. This is used for printing the filename when printing the exception for clarity's sake. --- dotnet/DWXConnect/api/Helpers.cs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/dotnet/DWXConnect/api/Helpers.cs b/dotnet/DWXConnect/api/Helpers.cs index aefe61b..7f36dbc 100644 --- a/dotnet/DWXConnect/api/Helpers.cs +++ b/dotnet/DWXConnect/api/Helpers.cs @@ -81,31 +81,53 @@ public static string format(double value) */ public static string tryReadFile(string path) { + fileName = GetFileNameFromPath(path); + try { return File.ReadAllText(path); } catch (DirectoryNotFoundException) { - Console.WriteLine("api.Helpers.tryReadFile | DirectoryNotFoundException. Returning empty string."); + Console.WriteLine($"api.Helpers.tryReadFile | {fileName} | DirectoryNotFoundException. Returning empty string."); return ""; } catch (FileNotFoundException) { - Console.WriteLine($"api.Helpers.tryReadFile | FileNotFoundException. Creating empty file at path ({path}) & returning empty string."); + Console.WriteLine($"api.Helpers.tryReadFile | {fileName} | FileNotFoundException. Creating empty file at path ({path}) & returning empty string."); CreateEmptyFile(path); return ""; } catch (IOException) { - Console.WriteLine("api.Helpers.tryReadFile | IOException. Race condition. Most likely this process and the MetaTrader EA both trying to access/use the file simultaneously. Returning empty string."); + Console.WriteLine("api.Helpers.tryReadFile | {fileName} | IOException. Race condition. Most likely this process and the MetaTrader EA both trying to access/use the file simultaneously. Returning empty string."); return ""; } } - private static void CreateEmptyFile(string filepath) + private static void CreateEmptyFile(string filePath) { - File.Create(filepath).Dispose(); + File.Create(filePath).Dispose(); + } + + private static string GetFileNameFromPath(string path) + { + try + { + return path.Split("\\").Last(); + } + catch (Exception) + { + try + { + return path.Split("/").Last(); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } + } } } } From 0df070080d9def572cae48d73c4f3afa4ca7c8c3 Mon Sep 17 00:00:00 2001 From: Jonathon <34756063+spliffli@users.noreply.github.com> Date: Tue, 26 Mar 2024 23:34:40 +0100 Subject: [PATCH 4/5] Update Helpers.cs Added missing $ symbol before string --- dotnet/DWXConnect/api/Helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/DWXConnect/api/Helpers.cs b/dotnet/DWXConnect/api/Helpers.cs index 7f36dbc..bb776f5 100644 --- a/dotnet/DWXConnect/api/Helpers.cs +++ b/dotnet/DWXConnect/api/Helpers.cs @@ -100,7 +100,7 @@ public static string tryReadFile(string path) } catch (IOException) { - Console.WriteLine("api.Helpers.tryReadFile | {fileName} | IOException. Race condition. Most likely this process and the MetaTrader EA both trying to access/use the file simultaneously. Returning empty string."); + Console.WriteLine($"api.Helpers.tryReadFile | {fileName} | IOException. Race condition. Most likely this process and the MetaTrader EA both trying to access/use the file simultaneously. Returning empty string."); return ""; } } From d9be61500aa7beb8a184a24bf9ec98ddb1a0941c Mon Sep 17 00:00:00 2001 From: Jonathon <34756063+spliffli@users.noreply.github.com> Date: Wed, 27 Mar 2024 20:12:45 +0100 Subject: [PATCH 5/5] Added missing var keyword in Helpers.tryReadFile --- dotnet/DWXConnect/api/Helpers.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dotnet/DWXConnect/api/Helpers.cs b/dotnet/DWXConnect/api/Helpers.cs index bb776f5..982e113 100644 --- a/dotnet/DWXConnect/api/Helpers.cs +++ b/dotnet/DWXConnect/api/Helpers.cs @@ -81,7 +81,7 @@ public static string format(double value) */ public static string tryReadFile(string path) { - fileName = GetFileNameFromPath(path); + var fileName = GetFileNameFromPath(path); try {