Click here to Skip to main content
Rate this: bad
good
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
CGN0071.2K
Comments
SAKryukov at 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?

--SA
CGN007 at 24-Jan-12 3:45am
   
Sorry...
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
good
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)
{
        Dispatcher.Invoke((Action)delegate()
	{
 
		if(Your Conditions)
			YourMethod();
 
	});
}
  Permalink  
Comments
CGN007 at 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 at 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)
return;

If (CheckUsers())
YourMethod();


Then YourMethod() will be done at 12 O'clock everyday.
you can complite the code for performing the method only one time daily.
CGN007 at 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 at 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);
timer.Start();
}
CGN007 at 24-Jan-12 4:43am
   
Thanks...
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 at 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 at 24-Jan-12 5:26am
   
K...But..,
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 at 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(isIgnoreThis)
return;

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

return;


If (CheckUsers())

{
isIgnoreThis = true;
YourMethod();


}

Any more questions ?
CGN007 at 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 at 24-Jan-12 6:42am
   
It does not make any difference.

break your time to 2 variables. (hour , min)
then:
if (DateTime.Now.Hour != hour || DateTime.Now.Minute != min)
...
Shahin Khorshidnia at 24-Jan-12 6:49am
   
Did it help ?
CGN007 at 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 at 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
0 Sascha Lefévre 485
1 Sergey Alexandrovich Kryukov 325
2 Maciej Los 240
3 Abhinav S 164
4 Peter Leow 120
0 Sergey Alexandrovich Kryukov 6,953
1 OriginalGriff 6,311
2 Maciej Los 2,732
3 Peter Leow 2,674
4 Abhinav S 2,562


Advertise | Privacy | Mobile
Web01 | 2.8.150414.1 | Last Updated 24 Jan 2012
Copyright © CodeProject, 1999-2015
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