public MainWindow() { InitializeComponent(); ListMessages.ItemsSource = mes; Task.Run(() => { while (true) { for (int i = 0; i < 50; i++) { Thread.Sleep(10); this.Dispatcher.Invoke(() => { bar.Value++; }); } this.Dispatcher.Invoke(() => { bar.Value = 0; }); using (ChatEntities db = new ChatEntities()) { var a = db.Messages.ToList(); this.Dispatcher.Invoke(() => { mes.Clear(); foreach (var item in a) { mes.Add($"{item.Date}: {item.Text}"); } }); } } }); //using (ChatEntities db = new ChatEntities()) //{ // var a = db.Messages.ToList(); // foreach(var item in a) // { // mes.Add($"{item.Date}: {item.Text}"); // } //} }
private void Button_Click(object sender, RoutedEventArgs e) { Message m = new Message() { Text = MyText.Text, Date = DateTime.Now }; mes.Add($"{m.Date}: {m.Text}"); using (ChatEntities db = new ChatEntities()) { db.Messages.Add(m); db.SaveChanges(); } }
protected void Page_PreRender(object sender, EventArgs e) { var dbContext = new ChatEntities(); int messagesCount = dbContext.Messages.Count(); IQueryable <Message> messages = null; if (messagesCount > 15) { messages = dbContext.Messages.Include("User") .OrderBy(msg => msg.PostDate).Skip(messagesCount - 15).Take(15); } else { messages = dbContext.Messages.Include("User"); } this.ListViewMessages.DataSource = messages.ToList(); this.ListViewMessages.DataBind(); }
protected void OnBtnSendMessage_Click(object sender, EventArgs e) { var dbContext = new ChatEntities(); var currentUser = dbContext.Users.FirstOrDefault( u => u.UserName == User.Identity.Name); if (currentUser != null) { Message newMessage = new Message() { Text = this.TextBoxNewMessageText.Text, User = currentUser, PostDate = DateTime.Now }; dbContext.Messages.Add(newMessage); dbContext.SaveChanges(); this.TextBoxNewMessageText.Text = ""; } }