Skip to content

Commit

Permalink
Insert locks
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswendt1 committed Mar 27, 2021
1 parent 385ecad commit 47579b6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ TranslationAssistant.DocumentTranslationInterface/bin/Release/FileTranslator.vsh
/OpenXmlPowerTools/bin/Debug
/.vs/Microsoft.DocumentTranslator/DesignTimeBuild
.vs/
.vs/slnx.sqlite
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,10 @@ public void PopulateAvailableLanguages()
if (!TranslationServiceFacade.UseCustomEndpoint) this.sourceLanguageList.Add(Properties.Resources.Common_AutoDetect);
try
{
this.targetLanguageList.AddRange(TranslationServiceFacade.AvailableLanguages.Values);
lock (TranslationServiceFacade.AvailableLanguages)
{
this.targetLanguageList.AddRange(TranslationServiceFacade.AvailableLanguages.Values);
}
}
catch (Exception ex) {
this.StatusText = String.Format("{0}\n{1}", Properties.Resources.Error_LanguageList, ex.Message);
Expand Down
23 changes: 15 additions & 8 deletions TranslationServices.Core/TranslationServiceFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ public static async Task GetLanguages(string AcceptLanguage = "en")
/// <param name="AcceptLanguage">Accept-Language</param>
private static async Task GetLanguagesInternal(string AcceptLanguage = "en")
{
AvailableLanguages.Clear();
lock (AvailableLanguages)
{
AvailableLanguages.Clear();
}
if (UseCustomEndpoint)
{
ContainerGetLanguages();
Expand All @@ -390,17 +393,21 @@ private static async Task GetLanguagesInternal(string AcceptLanguage = "en")
request.Headers.Add("Accept-Language", AcceptLanguage);
HttpResponseMessage response = await client.SendAsync(request).ConfigureAwait(false);
string jsonResponse = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
lock (AvailableLanguages)
{
var result = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(jsonResponse);
var languages = result["translation"];

string[] languagecodes = languages.Keys.ToArray();
foreach (var kv in languages)
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
AvailableLanguages.Add(kv.Key, kv.Value["name"]);
var result = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, Dictionary<string, string>>>>(jsonResponse);
var languages = result["translation"];

string[] languagecodes = languages.Keys.ToArray();
foreach (var kv in languages)
{
AvailableLanguages.Add(kv.Key, kv.Value["name"]);
}
}
}
Debug.Assert(AvailableLanguages.Count > 0, "GetLanguagesInternal: Zero languages.");
}
catch (HttpRequestException)
{
Expand Down

0 comments on commit 47579b6

Please sign in to comment.