C# DateTime t1=(DateTime)rdv.Date; DateTime t2=DateTime.Now; 时间跨度t=t2-t1; if(总分钟数

C# DateTime t1=(DateTime)rdv.Date; DateTime t2=DateTime.Now; 时间跨度t=t2-t1; if(总分钟数,c#,multithreading,timer,task,C#,Multithreading,Timer,Task,每当您想要与UI线程所拥有的对象通信时,您都可以使用Dispatcher进行通信。尽管如此,我真的不知道你为什么要用这种方式编写代码。但是我只是为您的具体问题提供了一个答案。为什么您认为线程会继续执行该方法?线程将在该方法返回时结束。您可能希望尝试减少您的线程数。是的,我理解这一点,并且我尝试找到一个解决方案来执行它几次或每分钟一次,例如。。。。 public partial class MenuPrincipal : Window { public MenuPrincipal()

每当您想要与
UI
线程所拥有的对象通信时,您都可以使用
Dispatcher
进行通信。尽管如此,我真的不知道你为什么要用这种方式编写代码。但是我只是为您的具体问题提供了一个答案。

为什么您认为线程会继续执行该方法?线程将在该方法返回时结束。您可能希望尝试减少您的线程数。是的,我理解这一点,并且我尝试找到一个解决方案来执行它几次或每分钟一次,例如。。。。
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++;
                                }
                            }
                        }
                    }
                }
            }
    }
}
private void notification()
{
    //do your logic here

    Thread.Sleep(1000); //this time is every second (1000 ms)
}
//This should be stored in the resources, or some kind of configuration class
        public string con = $@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)}\MCDatabase.mdf;Integrated Security=True";
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;
    Task.Factory.StartNew(notification, TaskCreationOptions.LongRunning);
}
private async void notification()
        {

            MCDataClassDataContext dataClass = new MCDataClassDataContext(con);
            while (true)
            {
                await Task.Delay(TimeSpan.FromSeconds(3));

                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++;
                                    }
                                }
                            }
                        }
                    }
                }

        }