【发布时间】:2018-03-27 15:28:16
【问题描述】:
我有一个使用 WPF 的 c# 应用程序。我有一个方法 notification(); (如果我的数据库中有一个即将到来的约会显示通知)并且我想在我的应用程序运行时并行执行它几次(不停止),以便它可以随时检查数据库! 我在网上搜索并尝试了许多解决方案,例如线程,计时器,但它只并行执行一次!
public partial class MenuPrincipal : Window
{
public MenuPrincipal()
{
InitializeComponent();
UserControl usc = new Menu(ListRDV);
SelectionGrid.Children.Add(usc);
usc.VerticalAlignment = VerticalAlignment.Stretch;
usc.HorizontalAlignment = HorizontalAlignment.Stretch;
WindowStartupLocation = WindowStartupLocation.CenterScreen;
Globals.TempcalculNotif = 60;
Globals.TempRappelRDV = 15;
Thread thrdNotif = new Thread(notification);
thrdNotif.Start();
}
private void notification()
{
MessageBox.Show("calcul notif");
string con = $@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}\MCDatabase.mdf;Integrated Security=True";
MCDataClassDataContext dataClass = new MCDataClassDataContext(con);
IQueryable<RendezVous> NotifRdv = (from rendezVous in dataClass.RendezVous
orderby rendezVous.Date
select rendezVous);
int cpt = 0;
if (NotifRdv.Count() != 0)
{
foreach (RendezVous rdv in NotifRdv)
{
string dateRdv, dateAJRD;
dateRdv = rdv.Date.ToString().Substring(0, 10);
dateAJRD = DateTime.Today.ToString().Substring(0, 10);
if (string.Compare(dateRdv, dateAJRD) == 0)
{
DateTime t1 = (DateTime)rdv.Date;
DateTime t2 = DateTime.Now;
TimeSpan t = t2 - t1;
if (t.TotalMinutes < Globals.TempRappelRDV)
{
if (rdv.IdPatient != 0)
{
if (rdv.Important == true)
{
TextBlock TextRDV = new TextBlock();
IQueryable<Patient> patientRDV = (from patient in dataClass.Patient
where rdv.IdPatient == patient.Id
select patient);
IQueryable<Personne> personneRDV = (from personne in dataClass.Personne
where patientRDV.First().IdPersonne == personne.Id
select personne);
string NomPatient = personneRDV.First().nom;
string PrenomPatient = personneRDV.First().prenom;
string heureRDV = rdv.Date.ToString().Substring(10, 9);
TextRDV.Text = " RENDEZ VOUS DANS " + Globals.TempRappelRDV.ToString() + " min \n Patient: \n Nom: " + NomPatient + "\n Prenom: " + PrenomPatient + "\n Heure: " + heureRDV + "\n\t\t" + DateTime.Now.ToString();
ListeNotif.Items.Add(TextRDV);
System.Windows.Forms.NotifyIcon notif = new System.Windows.Forms.NotifyIcon();
notif.Visible = true;
notif.Icon = new System.Drawing.Icon(@"../../ressources/Icones/icones ico/logo_white.ico");
notif.ShowBalloonTip(5000, "Medicare", "Vous un rendez vous important dans " + Globals.TempRappelRDV.ToString() + " minutes ", System.Windows.Forms.ToolTipIcon.Info);
cpt++;
}
}
}
}
}
}
}
}
【问题讨论】:
-
为什么你认为线程会不断地执行方法?线程将在此方法返回的那一刻结束。
-
你可能想尝试减少你的Arrow Code。
-
是的,我明白这一点,我试图找到一个解决方案来执行它几次或每分钟一次,例如......
标签: c# multithreading timer task