// 1. post session request public PleaseWaitWindow Initialize(Account sender, Account receiver, Action closeWatcher, object locker) { _closeWatcher = closeWatcher; _locker = locker; SessionController.CreateSessionRequestResponse(sender, receiver, false, _locker); Task.Run(async() => { string text = ""; if (Dispatcher != null) { await Dispatcher?.InvokeAsync(() => text = ConnectionBlock.Text); var i = 30; while (i > 0 && !_close) { var secs = i; // ReSharper disable once PossibleNullReferenceException await Dispatcher.InvokeAsync(() => ConnectionBlock.Text = text + secs + "sec"); await Task.Delay(1000); i--; } Close(); } _closeWatcher(); }); Task.Run(() => FSWatcherController.ExecuteWatcher(_fileSystemWatcher, Directory.GetCurrentDirectory() + @"\..\..\ChatRequests\", "*.sesres", ResponseCallback, () => _close)); return(this); }
// 2. Get session request // 3. Post session response private void ChatRequestIsHere(object o, FileSystemEventArgs e) { if (Path.GetFileNameWithoutExtension(e.Name) != AccountsController.GetInstance().CurrentAccount.Username) { return; } Account receiver = AccountsController.GetInstance().CurrentAccount; Account sender = SessionController.ReadSessionRequest(AccountsController.GetInstance().CurrentAccount, false, _locker); if (sender == null) { return; } var result = MessageBox.Show($"Do you want to chat with {sender.Username}?", "Chat request", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result != MessageBoxResult.Yes) { return; } if (!SessionController.CreateSessionRequestResponse(receiver, sender, true, _locker)) { return; } CloseRequest = true; CloseOnline = true; _fileSystemWatcherOnline.EnableRaisingEvents = false; _fileSystemWatcherOnline.Dispose(); _fileSystemWatcherRequest.EnableRaisingEvents = false; _fileSystemWatcherRequest.Dispose(); lock (_locker) { while (true) { try { File.Delete(USER_REQUEST_PATH); break; } catch (IOException) { Task.Delay(1000); } } } ChatWindow chatWindow = null; if (Dispatcher != null) { Dispatcher.Invoke(() => chatWindow = new ChatWindow().Initialize(this, sender)); Task.Delay(500); Dispatcher.Invoke(() => chatWindow.Show()); } Dispatcher?.Invoke(Hide); }
protected override void OnClosing(CancelEventArgs e) { var result = MessageBox.Show($"Do you want to cancel chat session with {_receiver.Username}?", "Chat session", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes) { _close = true; SessionController.CreateSessionRequestResponse(AccountsController.GetInstance().CurrentAccount, _receiver, false, locker); ClosingController.CloseApplication(); } else { e.Cancel = true; } base.OnClosing(e); }