如何管理创建调度程序的线程? [英] How to manage threads for creating scheduler?

查看:101
本文介绍了如何管理创建调度程序的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户想要添加计划任务的应用程序,例如。 打电话给客户,提醒他支付服务费等等。

我用Google搜索,发现有几种方法可以创建提醒,如使用任务,计时器,线程,我是新手穿线我是什么创建的内容如下所述。我在这里解释了几个问题。



我创建了一个winform,它将接受提醒的日期时间和提醒的其他详细信息。在应用程序启动时,我正在检索当天的所有提醒,在我创建将在特定时间显示提醒的线程之后。我的代码如下:



I have an application in which user want add a schedule task eg. "call client", "remind him for service payment", etc.
I googled hard and found there are several ways for creating reminders like using Task, Timer, Thread, i am newbie in threading what i have created is explained below. There are several problem i'm facing explained here.

I have created a winform that will accept date time for reminder and and other details for reminder. On start of application i am retrieving all the reminders for that day, after i am creating thread that will show reminder for at specific time. my code is given below

public static void LoadScheduler()
{
      try
      {
         schedulerFullInfoBO = objAdvisorServiceClient.SchedulerInformationService(DAL.advisorID, DateTime.Now, DateTime.Now.AddDays(1), "Reminder", "");//this will get list of reminder for today
                
         if (tReminder != null) //tReminder is thread which will create threads for opening forms 
         {
             tReminder.Abort();
             tReminder = null;
         }
         SyncSchedule();//this function is calculating when to show form
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
}

public static void SyncSchedule()
{
   if (schedulerFullInfoBO != null && schedulerFullInfoBO.Length > 0)
   {
      schedulerFullInfoBO = schedulerFullInfoBO.OrderBy(us => us.schedulerBO.Reminder_On).ToArray();
      if ((schedulerFullInfoBO[0].schedulerBO.Reminder_On - DateTime.Now).Ticks > 0)
      {
         if (tReminder == null)
         { 
          tReminder = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ShowReminder));
          tReminder.IsBackground = true;
          tReminder.Start();
         }
       }
       else //this is for the condition if he has created reminder for specific time but app was close so on startup this will be show
       {
         frmReminder objReminder = new frmReminder(schedulerFullInfoBO[0]);
         objReminder.Show();
         schedulerFullInfoBO = schedulerFullInfoBO.Skip(1).ToArray();                    
         SyncSchedule();                
       }
    }
}
static void ShowReminder(object reminderFullInfoBO)
{
    while (schedulerFullInfoBO != null && schedulerFullInfoBO.Length > 0)
    {
        ts = new TimeSpan((schedulerFullInfoBO[0].schedulerBO.Reminder_On - DateTime.Now).Ticks >= 0 ? (schedulerFullInfoBO[0].schedulerBO.Reminder_On - DateTime.Now).Ticks : 0);
//now this will put thread sleep for specific timespan
        System.Threading.Thread.Sleep(ts);
//tReminder thread is maintaining other threads
        System.Threading.Thread threadOpenReminder = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(openReminder));
        threadOpenReminder.IsBackground = true;
        threadOpenReminder.Start(schedulerFullInfoBO[0]);
        schedulerFullInfoBO = schedulerFullInfoBO.Skip(1).ToArray();
    }
}
//below function will open reminder form
static void openReminder(object reminderFullInfoBO)
{
    SchedulerFullInfoBO objBO = reminderFullInfoBO as SchedulerFullInfoBO;
    frmReminder objReminder = new frmReminder(objBO);
    objReminder.ShowDialog();
}





代码工作正常,但当我添加了贪睡功能

如下,



The code is working fine but when i added snooze function
as below,

void snooze()
{
      TimeSpan ts = new TimeSpan((dtpReminder.Value - DateTime.Now).Ticks);
      System.Threading.Thread.Sleep(ts);
}



这使主线程处于睡眠状态。



如果想重新安排或推迟它不仅仅是我必须调用

LoadScheduler()函数并且整个过程重复它自己。

i不是能够管理所有线程。

我知道这是非常非常丑陋的代码,但我完全无能为力,如何管理线程,同步时间表和提醒。

寻求宝贵的帮助。提前谢谢。


this put main thread in sleep.

if want to reschedule it or postpone it than again i have to called
LoadScheduler() function and entire process repeat it self.
i am not able to manage all the thread.
I know this is very very ugly code but i'm completely clueless what and how to do it manage threads and sync schedules and reminder.
Seeking your valuable help. Thanks in advance.

推荐答案

当然你所尝试的是完全错误的,因为它只是让主线程处于休眠状态并保持一段时间没有响应。基本上,在UI线程中几乎任何使用 Sleep 都表示错误的方法。您的延后方法应该在系统的状态机中设置一些标志。显示提醒的代码应该读取此标志(可能还有其他数据,例如贪睡时间),并采取相应措施:取消提醒,重新安排,等等。



-SA
Of course what you have tried is totally wrong as it simply put the main thread to sleep and keep it non-responsive for a while. Basically, almost any use of the Sleep in the UI thread is the indication of wrong approach. Your snooze method should set some flag in the state machine of your system. The code showing the reminder should read this flag (and possibly other data, such as snooze time), and act accordingly: cancel reminder, reschedule it, and so on.

—SA


这篇关于如何管理创建调度程序的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆