private async void Run() { receiverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); localEP = new IPEndPoint(IPAddress.Any, PORT); receiverSocket.Bind(localEP); remoteEP = new IPEndPoint(IPAddress.Any, 0); await Task.Run(() => { while (true) { byte[] buff = new byte[500]; int len = receiverSocket.ReceiveFrom(buff, ref remoteEP); string msg = Encoding.Unicode.GetString(buff, 0, len); try { msg = msg.Insert(msg.IndexOf(':'), "]"); msg = msg.Insert(0, "["); msg = msg.Replace(':', ' ').Replace(';', ' '); } catch (Exception) { } if (msg.Contains(userName)) { MessageInfo msgInfo = new MessageInfo { Text = msg, Time = DateTime.Now.ToShortTimeString() }; Dispatcher.BeginInvoke(new Action(() => { MessageControl mc = new MessageControl() { MsgInfo = msgInfo }; mc.ApplyStyles(ReceiverOrSender.Sender); stackPanelMsgs.Children.Add(mc); })); } else { MessageInfo msgInfo = new MessageInfo { Text = msg, Time = DateTime.Now.ToShortTimeString() }; Dispatcher.BeginInvoke(new Action(() => { MessageControl mc = new MessageControl() { MsgInfo = msgInfo }; mc.ApplyStyles(ReceiverOrSender.Receiver); stackPanelMsgs.Children.Add(mc); })); } } }); }
private void Test() { Messages.AddRange(new MessageInfo[] { new MessageInfo { Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.", Time = DateTime.Now.ToShortTimeString() }, new MessageInfo { Text = "Some text", Time = DateTime.Now.ToShortTimeString() }, new MessageInfo { Text = "Some text", Time = DateTime.Now.ToShortTimeString() } }); int counter = 0; foreach (var item in Messages) { if (counter % 2 == 0) { MessageControl mc = new MessageControl() { MsgInfo = item }; mc.ApplyStyles(ReceiverOrSender.Receiver); stackPanelMsgs.Children.Add(mc); } else { MessageControl mc = new MessageControl() { MsgInfo = item }; mc.ApplyStyles(ReceiverOrSender.Sender); stackPanelMsgs.Children.Add(mc); } counter++; } }