Click here to Skip to main content
15,918,211 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello,

How to execute a method based on specific date in c#? like i have a method and i want to execute it on a particular date.

Thanks.
Posted

Basically, you need to have some Windows Service which could start an application on certain conditions.

Such service already exists, It is designed to support really complex schedules. It is already bundled with Windows and enabled; you can use it on different levels. It is called Window Task Scheduler, see http://en.wikipedia.org/wiki/Windows_Task_Scheduler[^].

First, you can schedule events using command-line utilities AT.EXE or CSHTASKS.EXE (which is replacing AT.EXE), see:
http://en.wikipedia.org/wiki/At_%28Windows%29[^],
http://en.wikipedia.org/wiki/Schtasks[^],
http://technet.microsoft.com/en-us/library/bb490866.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx[^].

And you also can use Window Task Scheduler API, please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383614%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383608%28v=vs.85%29.aspx[^].

To see how can you use it with .NET, see this CodeProject article: A New Task Scheduler Class Library for .NET[^].

[EDIT]

In response to follow-up discussion: please see:
http://en.wikipedia.org/wiki/Push_technology[^].

—SA
 
Share this answer
 
v2
Comments
Member 8145305 12-Mar-12 4:08am    
Method will be on a web server in an asp.net application. so i can't use this windows service thing.
Sergey Alexandrovich Kryukov 12-Mar-12 4:41am    
You should have mentioned that.
You can use it on server-side, why not? The only problem is that you hardly can solve this problem in ASP.NET, no matter what method you would use. This is because it uses pull technology, not push. There is a technology for server push, but it's not universally supported.
--SA
Sergey Alexandrovich Kryukov 12-Mar-12 4:43am    
...however, with scheduler, you can use it first time anyone sends and HTTP request to the server.
Generally, it makes no sense for ASP.NET at all. This is basically a pure client-server model, based on pull. :<
--SA
As already suggested you can make use of Windows service here to perform the task on the desired time.

Your comment stated this event handler/method resides in the ASP.Net application. Now, if there nothing really in the ASP.Net application that triggers it you do not need to have the method in it. You can create a new Windows service that runs on the server and do processing as and when needed.
 
Share this answer
 

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