Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
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

C#
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,
C#
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.
Posted
Comments
gggustafson 14-May-14 14:19pm    
Are you familiar with services?
Kalpesh Bhadra 15-May-14 1:30am    
@ggustafson: sorry i dont know about services. can you tell me what is it and how it can help me. i googled and found this http://www.codeproject.com/Articles/3938/Creating-a-C-Service-Step-by-Step-Lesson-I but i think this will not help me.
gggustafson 15-May-14 11:18am    
A service is software that executes independently of user interaction. For example, on my machine (Windows 7 Professional), I have installed avast! antivirus. There are two important parts to that application: the user interface that allows me to set preferences and the service that starts when I logon (with no action on my part) and then watches the data that comes to me over the Internet. In the event that something bad happens or that I should take some action, the service raises an alert as a popup in the right side of the task bar.

I would suggest that you change your architecture to be service-oriented rather than thread-oriented. For example, you would have a WinForm application that a user can execute that collects the desired information for a reminder (datetime of the alarm, alarm delivery method (vibrate, audible sound, screen flash, email, etc.), an optional event description, etc.) and saves the alarm data in a location where both the application and service can access it. The application wakes up the service so that it can access the alarm data and schedule a wake up for the next alarm datetime. When the service wakes up, it alerts the user of the alarm and again schedules a wake up for the next alarm datetime.

Services can be built either from scratch or through Visual Studio.

A discussion of Visual Studio services can be found at http://msdn.microsoft.com/en-us/library/y817hyb6(v=vs.90).aspx. Additional information can be found by Googling "C# services". Read http://stackoverflow.com/questions/12885013/windows-service-with-timer.

Good Luck.

1 solution

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
 
Share this answer
 
Comments
gggustafson 14-May-14 14:21pm    
Isn't this really a comment. By making it a solution, you removed the question from the unanswered.

Regards
Sergey Alexandrovich Kryukov 14-May-14 14:50pm    
I really believe this is all the help OP needs, otherwise I would put it as comment. How would you imagine "real" answer? Fixing the fragment of the code shown? Look at this fragment; and you will see that it hardly can be appropriate. I put my advise just because I think this is the best advice, not just because I would be harder for me to provide more detail.

I'm not overusing answers. There are many, many answers just saying "We don't do your homework. This is set on purpose...", but I only comment on such cases and provide some advise in just comments.

If we followed your way of thinking, almost all of the "Solutions" would be considered as comments, as we quite rarely post the whole source code to solve the problem comprehensively. (And I do have such posts.) Some posts are certainly not answers and got abuse reports (actually, a big number of members only posts non-answers as "Solutions", and many already earned abuse reports on their account for solution cheating). But in the cases on the wedge between answers and comment I never dispute; it just would be pointless.

I think it's important to understand that the main purpose is not providing some "answers" but try to provide some help, something helping doing programming or learning programming. Very basically, if the advice is about asking questions in a better way, this should be a comment; if this is some help addressing the OP's concern or difficulties about programming or computer science, it can be an answer.

Thank you,
—SA
gggustafson 14-May-14 15:52pm    
We agree to disagree.
Sergey Alexandrovich Kryukov 14-May-14 17:17pm    
Good to hear your opinion.
Thank you.
—SA
Kalpesh Bhadra 15-May-14 1:39am    
@SergeyAlexandrovichKryukov: thank you for your suggestion. I am not able to manage the which thread is responsible for reminders and although i keep the track of threads i dont know how to put specific thread in sleep. it would be me pleasure If you can share me article or snippet how to manage them as i dont know what is exact problem with my code. Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900