Я хочу получать список уведомлений каждые 15 секунд из моей базы данных, не делая мою программу очень медленной
Этот список уведомлений будет отображаться с помощью всплывающего уведомления
Проблема, с которой я сталкиваюсь, заключается в том, что при вызове метода для отображения всплывающих окон, если всплывающее окно отображается во вторичном потоке, а не в основном потоке
Я пробовал много способов, как использовать событие и вызывать событие всякий раз, когда прошло время, но просто вызывать обработчик события во вторичном потоке, единственное, чего я смог достичь, это использовать окно сообщения в тот же метод, что и у всплывающего уведомления, но выглядит он плохо и не поддается целям бэкэнда
// This is the form that should show the notifications
private void Student_Details_Load(object sender, EventArgs e)
{
WinAPI.AnimateWindow(this.Handle, 200, WinAPI.BLEND);
notifications.NewNotification += OnNewNotification;
//This is a list of notifiactions
allNotifications = notifications.GetNotifications(StudentFound.Student.Person_ID);
// calling the method to start the thread
notifications.ShowNotifications();
//This was an attempt to get it right
//Thread notififcationThread = new Thread(new ThreadStart(() =>
//{
// while (true)
// {
// AllNotifications = new
Notifications().GetNotifications(StudentFound.Student.Person_ID);
// //MethodInvoker invoker = new
MethodInvoker(showNotifications);
// //invoker.Invoke();
// //showNotifications();
// Thread.Sleep(5000);
// }
//}));
//notififcationThread.Start();
}
public void OnNewNotification(object sender,EventArgs e)
{
MessageBox.Show("You have notifications");
PopupNotifier popup = new PopupNotifier();
popup.Image = Properties.Resources.About_104px;
popup.TitleText = "Smart Shool System";
foreach (Notifications item in allNotifications)
{
popup.ContentText = item.Notification_Text;
popup.Popup();
}
}
// The method in the class that pulls the data
public List<Notifications> GetNotifications(string userFound)
{
List<Notifications> allNotifications = new List<Notifications>();
DataHandler dataHandler = new DataHandler();
DataTable dataTable = new DataTable();
dataTable = dataHandler.GetNotifications(userFound);
foreach (DataRow rowItem in dataTable.Rows)
{
allNotifications.Add(new Notifications(int.Parse(rowItem["Notification_Id"].ToString()),
rowItem["Notification_text"].ToString(),
rowItem["Person_ID"].ToString(),
bool.Parse(rowItem["Notification_Status"].ToString())));
}
return allNotifications;
}
//EventHandler
private void OnNewNotification()
{
if (NewNotification !=null)
{
NewNotification(this, EventArgs.Empty);
}
}
//This is my current attempt this method is called in the frontend
public void ShowNotifications()
{
//OnNewNotification();
Thread notififcationThread = new Thread(new ThreadStart(() =>
{
while (true)
{
OnNewNotification();
Thread.Sleep(5000);
}
}));
notififcationThread.Start();
}