private async void Window_Loaded(object sender, RoutedEventArgs e) { int num = await UserForMessage.GetUnReadedMessages(); txtMessage.Text = num.ToString(); brdrMessage.Visibility = num > 0 ? Visibility.Visible : Visibility.Collapsed; }
public MainWindow() { InitializeComponent(); Task.Run(() => UserForMessage.AllUsers = UserForMessage.GetAllUsers()); Task.Run(() => DBMain.UpdateAllPregnant()); this.MaxHeight = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height; btns = new List <Button>() { btnSearch, btnAddFamily, btnOrphans, btnInventory, btnSystem, btnSettings }; btnUser.Content = BaseDataBase.CurrentUser.Name; txtClock.Text = DateTime.Now.ToString("t") + " " + DateTime.Now.ToString("d"); DispatcherTimer dt = new DispatcherTimer() { Interval = TimeSpan.FromMinutes(1), IsEnabled = true }; dt.Tick += async delegate { txtClock.Text = DateTime.Now.ToString("t") + " " + DateTime.Now.ToString("d"); int num = await UserForMessage.GetUnReadedMessages(); txtMessage.Text = num.ToString(); brdrMessage.Visibility = num > 0 ? Visibility.Visible : Visibility.Collapsed; }; //sign out when application ilde 10 minutes mIdle = new DispatcherTimer() { Interval = TimeSpan.FromMinutes(10), IsEnabled = true }; mIdle.Tick += delegate { mIdle.Stop(); if (!SignInUser.IsActivated) { SignInUser w = new SignInUser(false); w.ShowDialog(); if (w.IsAccepted) { mIdle.Start(); } } else { mIdle.Start(); } }; InputManager.Current.PreProcessInput += OnActivity; }
//static public static List <UserForMessage> GetAllUsers() { List <UserForMessage> users = new List <UserForMessage>(); SqlConnection con = new SqlConnection(BaseDataBase.ConnectionString); SqlCommand com = new SqlCommand($"select ID, Name, dbo.fn_LastMessage({BaseDataBase.CurrentUser.ID},ID) LastMessage, dbo.fn_LastMessageDate({BaseDataBase.CurrentUser.ID},ID) LastMessageDate from [Users]", con); try { con.Open(); SqlDataReader rd = com.ExecuteReader(); while (rd.Read()) { if ((int)rd["ID"] == BaseDataBase.CurrentUser.ID) { continue; } UserForMessage x = new UserForMessage(); if (!(rd["ID"] is DBNull)) { x.ID = System.Int32.Parse(rd["ID"].ToString()); } x.Name = rd["Name"].ToString(); if (rd["LastMessage"].ToString() != "") { x.lastMessage = new UserMessage(); x.lastMessage.Message = rd["LastMessage"].ToString(); x.lastMessage.Date = (DateTime)rd["LastMessageDate"]; } users.Add(x); } rd.Close(); } catch { users = null; } finally { con.Close(); } return(users); }
public void StartDispatcher() { if (dt == null) { dt = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(10), IsEnabled = true } } ; dt.Tick += async delegate { var u = lbUsers.SelectedItem as UserForMessage; if (u != null) { int num = await UserForMessage.GetUnReadedMessages(); if (u.UnreadedMessages > 0) { u.MakeMessagesReaded(); bool isAdded = await u.GetLastMessages(); if (isAdded) { messagesItems.Items.Refresh(); } } u.CheckMessages(); } if (UserForMessage.AllUsers.Sum(x => x.UnreadedMessages) > 0) { ICollectionView view = CollectionViewSource.GetDefaultView(lbUsers.ItemsSource); view.SortDescriptions.Clear(); view.SortDescriptions.Add(new SortDescription("UnreadedMessages", ListSortDirection.Descending)); lbUsers.Items.Refresh(); } }; dt.Start(); }