Implement IDisposable pattern on HttpClient usage

The HttpClient instances are now wrapped in a using statement to properly dispose them after use. This practice prevents potential memory leaks and ensures that system resources are returned back to the system once the HttpClient instances are no longer needed, improving the system's overall efficiency and performance.

Signed-off-by: Ivandro Jao <ivandrofly@gmail.com>
This commit is contained in:
Ivandro Jao 2024-06-21 19:44:23 +01:00
parent a9d737aff5
commit 761493d24c

View File

@ -933,7 +933,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioCoqui(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(nikseComboBoxVoice.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? nikseComboBoxVoice.Text : "http://localhost:5002/api/tts"); httpClient.BaseAddress = new Uri(nikseComboBoxVoice.Text.StartsWith("http", StringComparison.OrdinalIgnoreCase) ? nikseComboBoxVoice.Text : "http://localhost:5002/api/tts");
progressBar1.Value = 0; progressBar1.Value = 0;
@ -977,10 +978,12 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private bool GenerateParagraphAudioAllTalk(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioAllTalk(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("http://127.0.0.1:7851"); httpClient.BaseAddress = new Uri("http://127.0.0.1:7851");
progressBar1.Value = 0; progressBar1.Value = 0;
@ -1054,6 +1057,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private bool GenerateParagraphAudioElevenLabs(Subtitle subtitle, bool showProgressBar, string overrideFileName) private bool GenerateParagraphAudioElevenLabs(Subtitle subtitle, bool showProgressBar, string overrideFileName)
{ {
@ -1064,7 +1068,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return false; return false;
} }
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("xi-api-key", nikseTextBoxApiKey.Text.Trim()); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("xi-api-key", nikseTextBoxApiKey.Text.Trim());
@ -1131,6 +1136,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private List<AzureVoiceModel> GetAzureVoices(bool useCache) private List<AzureVoiceModel> GetAzureVoices(bool useCache)
{ {
@ -1233,7 +1239,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return false; return false;
} }
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "ssml+xml"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "ssml+xml");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "audio/mpeg");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Microsoft-OutputFormat", "audio-16khz-32kbitrate-mono-mp3"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-Microsoft-OutputFormat", "audio-16khz-32kbitrate-mono-mp3");
@ -1301,6 +1308,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
return true; return true;
} }
}
private void buttonOK_Click(object sender, EventArgs e) private void buttonOK_Click(object sender, EventArgs e)
{ {
@ -1806,7 +1814,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
var url = "https://huggingface.co/rhasspy/piper-voices/resolve/main/voices.json?download=true"; var url = "https://huggingface.co/rhasspy/piper-voices/resolve/main/voices.json?download=true";
@ -1823,6 +1832,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {
@ -1903,7 +1913,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
@ -1927,6 +1938,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {
@ -1991,7 +2003,8 @@ namespace Nikse.SubtitleEdit.Forms.Tts
if (!useCache) if (!useCache)
{ {
var httpClient = new HttpClient(); using(var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("accept", "application/json");
@ -2010,6 +2023,7 @@ namespace Nikse.SubtitleEdit.Forms.Tts
File.WriteAllBytes(jsonFileName, bytes); File.WriteAllBytes(jsonFileName, bytes);
} }
}
if (File.Exists(jsonFileName)) if (File.Exists(jsonFileName))
{ {