Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
i have asp.net web application. Every 12 hours i want to do something - check some data and then send the email. Is threading good soluton for this. Does anyone has an example?

If i do like that
C#
public delegate void Worker();
private static Thread worker;

public static void Init(Worker work)
{
    worker = new Thread(new ThreadStart(work));
    worker.Start();
}

public static void Work()
{
    AppLogic.SendMailTo("Something", "Something", true, "some.mail@gmail.com");

}

and call whis thread
C#
Init(Work);

Then asp send mail every time, when someone visits this page. How can i send this email every 12 hurs?

Thanks
Posted
Comments
[no name] 24-Jun-14 10:04am    
No. Threading is not a good solution for this. Using a website as a scheduling device is bad design. Use a scheduled task.
vezo11 24-Jun-14 10:18am    
Ok, thanks!

No, threading is NOT a good solution to this. ASP.NET code only runs when a request is received from a browser. It's not running all the time.

This requirement is better done as a console application executed by the Scheduled Task service in Windows.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Jun-14 11:19am    
Correct, a 5.
—SA
vezo11 24-Jun-14 12:53pm    
Thanks. I use this solution link. But in Global.asax file i have Application Language="C#" Inherits="AspDotNetStorefront.Global". If i remove "Inherits" this solution work, but other sites doesn work... Do you have any idea why or if can i move this soluton code in any other file?
Dave Kreskowiak 24-Jun-14 12:58pm    
This doesn't make any sense. All you need is the two methods in the Global.asax file. What you're talking about isn't even part of Global.asax and would have no impact at all on how Global.asax runs.


vezo11 24-Jun-14 13:06pm    
I added the same code that you can see in the link above. Then i add breakpoint in line with code myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed); and this exception does never fall. If i remove this Inherits, the program stops at exceptions and then give mi null exception, because they dont find some file.
Dave Kreskowiak 24-Jun-14 13:03pm    
On top of that, the code you found fires the Timer every 5 seconds. The Elaplsed code should be checking the current time against the scheduled time to determine if the code you want to run on the schedule should be executed.

If you didn't do that and just set the Timer for 12 hours, you'd be getting a 12-hour delay before your "scheduled code" would execute, starting from the time the first request comes in to your website.
See my past answer - How to send Automatic Messages from Asp.net C#?[^].

You should use a Task Scheduler for this type of Tasks.
 
Share this answer
 
Comments
vezo11 24-Jun-14 10:17am    
Thanks! In your past answer i find this Link, do you think like that?
You can try that.
vezo11 24-Jun-14 13:23pm    
Yes i try and i have a problem. Read above, if you can help. Thanks

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