From f6105f10ac6027c90ccadcbe8d2e6f577bed47d5 Mon Sep 17 00:00:00 2001 From: TheJoKlLa Date: Tue, 31 May 2022 12:35:15 +0200 Subject: [PATCH] Fixed PageDialoge Threads --- Borepin/Borepin/Base/ConnectionModelBase.cs | 33 +++++++++++++++++-- .../AddServerProcess/AuthPlainPageModel.cs | 27 +++++++++++---- .../AddServerProcess/SelectServerPageModel.cs | 16 ++++++--- Borepin/Borepin/PageModel/ServerPageModel.cs | 18 ++++++---- 4 files changed, 74 insertions(+), 20 deletions(-) diff --git a/Borepin/Borepin/Base/ConnectionModelBase.cs b/Borepin/Borepin/Base/ConnectionModelBase.cs index 1bed4f5..685f478 100644 --- a/Borepin/Borepin/Base/ConnectionModelBase.cs +++ b/Borepin/Borepin/Base/ConnectionModelBase.cs @@ -32,10 +32,28 @@ namespace Borepin.Base { case ConnectionStatusChange.Connected: IsConnected = true; - await LoadAPIData().ConfigureAwait(false); + try + { + await LoadAPIData().ConfigureAwait(false); + } + catch + { + IsConnected = false; + await _API.Disconnect().ConfigureAwait(false); + _API.UnbindAllEvents(); + } break; case ConnectionStatusChange.Reconnected: - await ReloadAPIData().ConfigureAwait(false); + try + { + await ReloadAPIData().ConfigureAwait(false); + } + catch + { + IsConnected = false; + await _API.Disconnect().ConfigureAwait(false); + _API.UnbindAllEvents(); + } break; case ConnectionStatusChange.ConnectionLoss: try @@ -84,7 +102,16 @@ namespace Borepin.Base if(_API.IsConnected) { - await LoadAPIData().ConfigureAwait(false); + try + { + await LoadAPIData().ConfigureAwait(false); + } + catch + { + IsConnected = false; + await _API.Disconnect().ConfigureAwait(false); + _API.UnbindAllEvents(); + } } } #endregion diff --git a/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs b/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs index ab682c6..9ca3627 100644 --- a/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerProcess/AuthPlainPageModel.cs @@ -89,7 +89,11 @@ namespace Borepin.PageModel.AddServerProcess if (_API.IsConnected) { - await _API.Disconnect().ConfigureAwait(false); + if (_API.IsConnected) + { + await _API.Disconnect().ConfigureAwait(true); + _API.UnbindAllEvents(); + } } try @@ -98,23 +102,32 @@ namespace Borepin.PageModel.AddServerProcess } catch (ConnectingFailedException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); - IsBusy = false; + IsBusy = false; + }); return; } catch (AuthenticationFailedException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false); - IsBusy = false; + IsBusy = false; + }); return; } catch(Exception) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unexpected Error.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unexpected Error.", "Ok").ConfigureAwait(false); - IsBusy = false; + IsBusy = false; + }); return; } diff --git a/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs b/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs index 2fbb07e..9b7c3dd 100644 --- a/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs +++ b/Borepin/Borepin/PageModel/AddServerProcess/SelectServerPageModel.cs @@ -90,9 +90,13 @@ namespace Borepin.PageModel.AddServerProcess } catch (UriFormatException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Server address is invaild.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Server address is invaild.", "Ok").ConfigureAwait(false); + + IsBusy = false; + }); - IsBusy = false; return; } @@ -103,9 +107,13 @@ namespace Borepin.PageModel.AddServerProcess } catch(ConnectingFailedException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); + + IsBusy = false; + }); - IsBusy = false; return; } diff --git a/Borepin/Borepin/PageModel/ServerPageModel.cs b/Borepin/Borepin/PageModel/ServerPageModel.cs index 3ef20a6..977582c 100644 --- a/Borepin/Borepin/PageModel/ServerPageModel.cs +++ b/Borepin/Borepin/PageModel/ServerPageModel.cs @@ -128,20 +128,26 @@ namespace Borepin.PageModel try { - await _API.Connect(Connection_Item).ConfigureAwait(true); + await _API.Connect(Connection_Item).ConfigureAwait(false); } catch(ConnectingFailedException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); - - IsBusy = false; + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to connect to server.", "Ok").ConfigureAwait(false); + + IsBusy = false; + }); return; } catch(AuthenticationFailedException) { - await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false); + Device.BeginInvokeOnMainThread(async () => + { + await _PageDialogService.DisplayAlertAsync("Connection failed", "Unable to authenticate to server.", "Ok").ConfigureAwait(false); - IsBusy = false; + IsBusy = false; + }); return; }