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", "");
if (tReminder != null)
{
tReminder.Abort();
tReminder = null;
}
SyncSchedule();
}
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
{
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);
System.Threading.Thread.Sleep(ts);
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();
}
}
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);
}
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.