Good day to all.
Not so long ago I started programming in C #. As a result, wrote the following: DIPlaylist - the program creates a DI.FM playlist for AIMP with activated premium for 7 days.
Since I study on my own, I cannot evaluate the correctness of the code. For the first time, I used async / await to ensure that the main tasks were performed outside the main thread, thereby preventing the main window of the program from hanging. But in the current form, I sometimes notice that it does happen.
As a result, I had a question, did I use async / await correctly? I will also be very grateful for comments on the rest of the code, it will be extremely useful for my training.
The code is small and in it I tried to comment on all significant and perhaps incomprehensible at first glance moments.
An example of such a code (clippings):
private async void btnStart_Click(object sender, RoutedEventArgs e) { progressBar.Value++; // ΠΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΏΠΈΡΡΠΌΠ° Ρ ΠΏΡΠ΅ΠΌΠΈΡΠΌ-ΡΡΡΠ»ΠΊΠΎΠΉ statusLabel.Content = await TempMail.GetLetter(); if (String.IsNullOrEmpty(Settings.TmLetterID)) { Status(false); return; } progressBar.Value++; // Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ ΠΈ ΡΠΎΡ
ΡΠ°Π½Π΅Π½ΠΈΠ΅ ΠΏΠ»Π΅ΠΉΠ»ΠΈΡΡΠ° statusLabel.Content = await Playlist.GoPlaylist(); if (statusLabel.Content.ToString().ToLower().Contains("ΠΎΡΠΈΠ±ΠΊΠ°")) { Status(false); return; } } public static async Task<string> GetLetter() { SetHeader(); // 10 ΠΏΠΎΠΏΡΡΠΎΠΊ Ρ Π·Π°Π΄Π΅ΡΠΆΠΊΠΎΠΉ Π² 3 ΡΠ΅ΠΊΡΠ½Π΄Ρ Π½Π° ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΏΠΈΡΡΠΌΠ° for (int i = 0; i < 10; i++) { await Task.Run(() => { Thread.Sleep(3000); }); string responseBody = ""; try { HttpResponseMessage response = await httpClient.GetAsync(Settings.TmURL); responseBody = await response.Content.ReadAsStringAsync(); } catch (Exception) { return "ΠΡΠΎΠ±Π»Π΅ΠΌΠ° Ρ Π΄ΠΎΡΡΡΠΏΠΎΠΌ ΠΊ " + Settings.TmURL; } Match match = new Regex("view\\/(.*?)\"", RegexOptions.IgnoreCase).Match(responseBody); if (match.Success) { Settings.TmLetterID = match.Groups[1].Value.Trim(); return "ΠΠΎΠ»ΡΡΠ΅Π½ΠΈΠ΅ ΠΊΠ»ΡΡΠ° Π½Π° Π²ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΉ ΠΏΡΠ΅ΠΌΠΈΡΠΌ..."; } } return "ΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΏΠΎΠ»ΡΡΠ΅Π½ΠΈΠΈ ΠΏΠΈΡΡΠΌΠ° Ρ Π°ΠΊΡΠΈΠ²Π°ΡΠΈΠ΅ΠΉ"; } public static async Task<string> GoPlaylist() { try { List<string> channelsInfo = new List<string>(); channelsInfo.Add($"#Name:Digitally Imported ({Settings.TmAddress}:{Settings.DiPass})"); channelsInfo.Add("#Cursor:-1"); JObject playlistJS = JObject.Parse(Settings.DiPlaylistJS); JToken[] channels = playlistJS["channels"].ToArray(); channelsInfo.Add($"#Summary:{channels.Count().ToString()} / 00:00:00:00 / 0 B"); channelsInfo.Add("#Flags:2047"); channelsInfo.Add("#Group:Radio|1"); int count = 0; return await Task.Run(() => { foreach (var data in channels) { count++; channelsInfo.Add($"#Track:{count}|http://prem2.di.fm:80/{data["key"].ToString()}_hi?{Settings.DiListenKey}||||{data["name"].ToString()}|0|0|||0|0|0"); } File.WriteAllLines($@"{Directory.GetCurrentDirectory()}\Digitally Imported.aimppl", channelsInfo); return "ΠΠ»Π΅ΠΉΠ»ΠΈΡΡ ΡΡΠΏΠ΅ΡΠ½ΠΎ ΡΠ³Π΅Π½Π΅ΡΠΈΡΠΎΠ²Π°Π½!"; }); } catch (Exception) { return "ΠΡΠΈΠ±ΠΊΠ° ΠΏΡΠΈ ΠΎΠ±ΡΠ°Π±ΠΎΡΠΊΠ΅ ΠΏΠ»Π΅ΠΉΠ»ΠΈΡΡΠ°"; } }
SetHeadermethod do? - VladDhttpClientthat is configured differently before each request? And what will happen if while waiting for the sending of one request another one will be sent? - VladD