private void StartReadingChatServer() { var cts = new CancellationTokenSource(); _ = m_chatService.ChatLogs() .ForEachAsync((x) => ChatHistory.Add($"{x.At.ToDateTime().ToString("HH:mm:ss")} {x.Name}: {x.Content}"), cts.Token); App.Current.Exit += (_, __) => cts.Cancel(); }
private void StartReadingChatServer() { var cts = new CancellationTokenSource(); _ = m_chatService.ChatLogs(new Username { Name = Name, }) .ForEachAsync((x) => { foreach (var newChat in FormatText(x.Time.ToDateTime().ToString("HH:mm:ss"), x.Name, x.Content)) { ChatHistory.Add(new Tuple <string, FontStyle, string, string>(newChat.Item1, newChat.Item2, newChat.Item3, newChat.Item4)); } }, cts.Token); App.Current.Exit += (_, __) => cts.Cancel(); }
static void Main(string[] args) { Console.Write("Enter your name> "); var name = Console.ReadLine(); if (string.IsNullOrWhiteSpace(name)) { name = "anonymous"; } Console.WriteLine($"Joined as {name}"); var chatServiceClient = new ChatServiceClient(); var consoleLock = new object(); // subscribe (asynchronous) _ = chatServiceClient.ChatLogs() .ForEachAsync((x) => { // if the user is writing something, wait until it finishes. lock (consoleLock) { Console.WriteLine($"{x.At.ToDateTime().ToString("HH:mm:ss")} {x.Name}: {x.Content}"); } }); // write while (true) { var key = Console.ReadKey(); // A key input starts writing mode lock (consoleLock) { var content = key.KeyChar + Console.ReadLine(); chatServiceClient.Write(new ChatLog { Name = name, Content = content, At = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime()), }).Wait(); } } }
static void Main(string[] args) { Console.Write("Enter your name> "); var name = Console.ReadLine(); if (string.IsNullOrWhiteSpace(name)) { name = "anonymous"; } Console.WriteLine($"Joined as {name}"); var chatServiceClient = new ChatServiceClient(); var consoleLock = new object(); _ = chatServiceClient.ChatLogs(new Username { Name = name, }) .ForEachAsync((x) => { lock (consoleLock) { Console.WriteLine($"{x.Time.ToDateTime().ToString("HH:mm:ss")} {x.Name}: {x.Content}"); } }); while (true) { var key = Console.ReadKey(); lock (consoleLock) { var content = key.KeyChar + Console.ReadLine(); chatServiceClient.Write(new ChatLog { Name = name, Content = content, Time = Timestamp.FromDateTime(DateTime.Now.ToUniversalTime()), }).Wait(); } } }