diff --git a/src/BreCalClient/MainWindow.xaml.cs b/src/BreCalClient/MainWindow.xaml.cs index 1a01de8..2385ce6 100644 --- a/src/BreCalClient/MainWindow.xaml.cs +++ b/src/BreCalClient/MainWindow.xaml.cs @@ -38,8 +38,9 @@ namespace BreCalClient #region Fields - private static Int32 _uiUpdateRunning = 0; - + //private static int _uiUpdateRunning = 0; + private static SemaphoreSlim uiLock = new SemaphoreSlim(1); + private Credentials? _credentials; private readonly ConcurrentDictionary _allShipcallsDict = new(); @@ -50,7 +51,7 @@ namespace BreCalClient private readonly UserApi _userApi; private readonly TimesApi _timesApi; private readonly StaticApi _staticApi; - private readonly ShipApi _shipApi; + private readonly ShipApi _shipApi; private CancellationTokenSource _tokenSource = new(); private LoginResult? _loginResult; @@ -91,16 +92,16 @@ namespace BreCalClient _staticApi = new StaticApi(Properties.Settings.Default.API_URL); _staticApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; _shipApi = new ShipApi(Properties.Settings.Default.API_URL); - _shipApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; + _shipApi.Configuration.ApiKeyPrefix["Authorization"] = "Bearer"; const int maxDelayInMilliseconds = 32 * 1000; var jitterer = new Random(); - var retryPolicy = + var retryPolicy = // Policy.Handle() Policy.HandleResult(resp => resp.StatusCode == HttpStatusCode.Unauthorized) //.OrResult - .WaitAndRetryAsync(1, + .WaitAndRetryAsync(1, retryAttempt => { var calculatedDelayInMilliseconds = Math.Pow(2, retryAttempt) * 1000; @@ -171,7 +172,7 @@ namespace BreCalClient this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this.LoadStaticLists(); - this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; + this.labelUsername.Text = $"{_loginResult.FirstName} {_loginResult.LastName}"; } } labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; @@ -211,9 +212,9 @@ namespace BreCalClient this._timesApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._shipcallApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; this._staticApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; - this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; + this._shipApi.Configuration.ApiKey["Authorization"] = _loginResult.Token; result = true; - } + } } else { @@ -237,7 +238,7 @@ namespace BreCalClient NewWithModel(null); } - private void NewWithModel(ShipcallControlModel? model) + private async void NewWithModel(ShipcallControlModel? model) { EditShipcallControl esc = new() { @@ -252,7 +253,9 @@ namespace BreCalClient // create UI & save new dialog model if (esc.ShipcallModel.Shipcall != null) { + await uiLock.WaitAsync(); this.UpdateUI(); + uiLock.Release(); esc.ShipcallModel.Shipcall?.Participants.Clear(); foreach (ParticipantAssignment pa in esc.ShipcallModel.AssignedParticipants.Values) @@ -346,10 +349,12 @@ namespace BreCalClient this.FilterShipcalls(); } - private void SearchFilterControl_SearchFilterChanged() + private async void SearchFilterControl_SearchFilterChanged() { this.FilterShipcalls(); + await uiLock.WaitAsync(); this.UpdateUI(); + uiLock.Release(); } private void checkboxShowCancelledCalls_Checked(object sender, RoutedEventArgs e) @@ -358,11 +363,13 @@ namespace BreCalClient this.SearchFilterControl_SearchFilterChanged(); } - private void comboBoxSortOrder_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) + private async void comboBoxSortOrder_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e) { _sortOrder = (Extensions.SortOrder) this.comboBoxSortOrder.SelectedIndex; this.FilterShipcalls(); + await uiLock.WaitAsync(); this.UpdateUI(); + uiLock.Release(); } private void buttonHistory_Click(object sender, RoutedEventArgs e) @@ -445,7 +452,7 @@ namespace BreCalClient this.Dispatcher.Invoke(new Action(() => { - labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; + labelGeneralStatus.Text = $"Connection {ConnectionStatus.SUCCESSFUL}"; labelLatestUpdate.Text = $"Last update: {DateTime.Now.ToLongTimeString()}"; labelStatusBar.Text = ""; generalProgressStatus.Value = 0; @@ -502,7 +509,9 @@ namespace BreCalClient } this.FilterShipcalls(); - this.UpdateUI(); + await uiLock.WaitAsync(); + this.UpdateUI(); + uiLock.Release(); } try @@ -665,14 +674,14 @@ namespace BreCalClient { _ = this._visibleControlModels.RemoveAll(x => { - Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY); + Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY); switch (x.Shipcall?.Type) { case ShipcallType.Arrival: - { + { if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value < sfm.EtaFrom; return x.Shipcall?.Eta < sfm.EtaFrom; - } + } default: // Shifting / Departing { if ((t != null) && t.EtdBerth.HasValue) return t.EtdBerth.Value < sfm.EtaFrom; @@ -689,7 +698,7 @@ namespace BreCalClient Times? t = x.GetTimesForParticipantType(ParticipantType.AGENCY); DateTime refValue = sfm.EtaTo.Value.AddMinutes(1439); // 23:59 switch (x.Shipcall?.Type) - { + { case ShipcallType.Arrival: { if ((t != null) && t.EtaBerth.HasValue) return t.EtaBerth.Value > refValue; @@ -701,7 +710,7 @@ namespace BreCalClient return x.Shipcall?.Etd > refValue; } } - }); + }); } if(sfm.MineOnly ?? false) @@ -765,7 +774,7 @@ namespace BreCalClient this.Dispatcher.Invoke(new Action(() => { - if (Interlocked.CompareExchange(ref _uiUpdateRunning, 1, 0) == 1) return; + //if (Interlocked.CompareExchange(ref _uiUpdateRunning, 1, 0) == 1) return; try { @@ -785,7 +794,7 @@ namespace BreCalClient } finally { - _uiUpdateRunning = 0; + // _uiUpdateRunning = 0; } Mouse.OverrideCursor = null; @@ -991,6 +1000,6 @@ namespace BreCalClient } #endregion - + } }