Click here to Skip to main content
15,884,677 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have two functions as following :

C#
public void SendMessage()
{
     //Some Code
}


AND

C#
public void CheckReceivedMessage()
{ 
     //Some Code
}


Now, I want CheckReceivedMessage(), this function should run continuously and SendMessage(), this function will run once a week which time will be set by user.

I want to use Threading for this.

For example, the user has set time Wednesday, 4:00 pm then every wednesday at 4:00 pm, SendMessage() will be executed and if today is friday & the user changes time & day from Wednesday to Sunday, then Upcoming sunday it should be executed...


And I have tried this for it....

C#
private void Form1_Load(object sender, EventArgs e)
{
     ThreadStart thrdLoop = new ThreadStart(ThreadLoop);
     Thread thrd1 = new Thread(thrdLoop);
     thrd1.Start();
}

public void ThreadLoop()
{
     while (true)
     {
          SendMessage();
          System.Threading.Thread.Sleep(TimeSpan.FromDays(7));
     }
}


But In above, when user will change time, thought SendMessage() will execute after 7 days after last execution...

Please guide me for this...

Thank you...
Posted
Updated 29-May-14 23:59pm
v2
Comments
ZurdoDev 30-May-14 7:57am    
Use a timer.
HardikPatel.SE 30-May-14 8:29am    
I have already tried but how to run CheckReceivedMessage() this function continuously???
ZurdoDev 30-May-14 8:31am    
That's what a timer is. It fires at a certain interval. Or, if you want it continuous just call it again after it is done.
HardikPatel.SE 30-May-14 8:46am    
Okay... I'll try bro.... Thanks... & yap by threading i am nearer to destination so if I'll not be success, I'll try timer.... Thanks again bro....

Have a look at quartz-scheduler: http://www.quartz-scheduler.net/[^]
It's an easy to use generic scheduler and you don not have to write logic for your time checking.

Hope it helps.
 
Share this answer
 
Comments
HardikPatel.SE 31-May-14 2:59am    
I have tried it but it's not opening any solutions in VS2010 or VS2012 so I am not able to check it's functionality.... Sorry bro....
Thread solution is not good idea because when user restarts the computer the time will be lost. I think another best solution will be use of Windows Task Scheduler. just set time and select what should be executed.
 
Share this answer
 
Comments
HardikPatel.SE 1-Jun-14 0:23am    
Yes but user will not provide Date, Year or Month to task scheduler... He/She will provide only Week Day and Time and also he/she will make change in it at any time....

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