Click here to Skip to main content
12,749,770 members (37,034 online)
Rate this:
Please Sign up or sign in to vote.
I want to call a specific function on my C#-WPF MVVM application at a specific time.I'm using System.Timers in My application.But later I realized that its impossible to achieve my goal using that.

Is anybody can suggest me a good solution for this.?
Thanks in advance...!!!
Posted 23-Jan-12 21:05pm
SAKryukov 24-Jan-12 2:14am
First, why would you do such thing? If you are trying to do anything weird, you should always explain why. Who would like to waste time on "how" it "what" is maybe wrong?

Second, "it's impossible" is simply not true. Who told you so? This is possible and even easy. So, please see what's your problem here?

CGN007 24-Jan-12 3:45am
Here is my problem details
I want to send mails by selecting any of the two options,
1.Each -- day of month
2.Each -- day of week
The user inputs which day and the start time...
Thanks for your time...!!!

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Why did you realize that System.Timers was impossible
But it works well for me!

void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)

		if(Your Conditions)

CGN007 24-Jan-12 3:47am
I want to send mails by selecting any of the two options, 1.Each -- day of month 2.Each -- day of week The user inputs which day and the start time... Its Problem...

Can it possible using the code...!!!
Shahin Khorshidnia 24-Jan-12 4:07am
Yes, make a method check all user's condition. for example CheckUsers().
You can also have a precondition.
for example:

if (DateTime.Now.Hour != 12)

If (CheckUsers())

Then YourMethod() will be done at 12 O'clock everyday.
you can complite the code for performing the method only one time daily.
CGN007 24-Jan-12 4:18am
I think I need to set the timer interval also...
How can I identify the interval value?
Please correct me ,If I'm wrong...
Shahin Khorshidnia 24-Jan-12 4:26am
private void Window_Loaded(object sender, RoutedEventArgs e)
timer = new System.Timers.Timer();
timer.Interval = 1800000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
CGN007 24-Jan-12 4:43am
I Think I can Identify the exact interval value like this
DateTime StartTime = DateTime.ParseExact("22:22:22", "HH:mm:ss", new DateTimeFormatInfo());
TimeSpan ts1 = StartTime.Subtract(DateTime.Now.ToLocalTime());
double Interval = ts1.TotalMilliseconds;
Shall I proceed like this?
Shahin Khorshidnia 24-Jan-12 5:00am
I don't have any idea why you are complicating the code? :D
If you want a correct period interval, you need to multiply the priod time (in second) by 1000.
for example if you need a 24 hours period time, then: 24*60*60 = 86400.
86400*1000 = 86400000

As a result:
timer.Interval = 86400000;
CGN007 24-Jan-12 5:26am
Suppose I want to execute the function at 8:30pm on every day...
I,m starting my application at 7:00am..
How can I Identify the Interval?
Hope u got the point...

My inputs are Day of Month And BeginExecution time...

Shahin Khorshidnia 24-Jan-12 6:28am
Set Interval at to 1 min (60000) if you exactly want to execute at 8:30

if(isIgnoreThis && DateTime.Now.Minute != 30)
isIgnoreThis = false;


if (DateTime.Now.Hour != 8 || DateTime.Now.Minute != 30)


If (CheckUsers())

isIgnoreThis = true;


Any more questions ?
CGN007 24-Jan-12 6:36am
Thanks for your time...
Anyway I want to generalize it,Since the time is not fixed...

Thanks again...
Shahin Khorshidnia 24-Jan-12 6:42am
It does not make any difference.

break your time to 2 variables. (hour , min)
if (DateTime.Now.Hour != hour || DateTime.Now.Minute != min)
Shahin Khorshidnia 24-Jan-12 6:49am
Did it help ?
CGN007 24-Jan-12 7:16am
Of course...Thanks a lot...!!!
Me used the code TimeSpan ts1 = StartTime.Subtract(DateTime.Now.ToLocalTime());
to calculate the Time interval...

Thanks for Your help...
Hope you'll find time to help in future also...!!!
Shahin Khorshidnia 24-Jan-12 7:54am
You're welcome.

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

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170215.1 | Last Updated 24 Jan 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100