Wants to call a function after time interval in ASP.net - asp.net

My problem is that I have a function implemented on my website which searches for Particular Tweets when I press the button. I want it to make it automatic such that, that function is called again and again after every two minutes, regardless some one uses the website or not..
I have a only a space piece of idea. Such as using a web service. Can any one help?

What you can do is add a System.Timers.Timer in Global.asax.
System.Threading.Timer timer = new System.Threading.Timer(new TimerCallback(TimerElapsed), null, new Timespan(0), new Timespan(24, 0, 0));
// This will run every 24 hours.
private void TimerElapsed(object o)
{
// Do stuff.
}

You could use a timer, declared in global.asax, like this:
void Application_Start(object sender, EventArgs e)
{
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 2000 * 60;
timer.Elapsed += Myhandler;
timer.Start();
Application.Add("timer", timer);
}
static void Myhandler(object sender, System.Timers.ElapsedEventArgs e)
{
}

Related

Wierd behavior while many clients generate cache

I show you my problem by simple code snippet.
This is popular scenario. Users load our page when there is no cache so we generate one. In my code example this take 120 seconds to save cache and before this i inrement static variable.
My qustion is why static variable "i" doesn't increment when i open this page many times in the same moment and cache is null.
public partial class _Default : Page
{
static int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
int i;
var cache = Cache.Get("cache") as string;
if (string.IsNullOrEmpty(cache))
{
i = GenerateCache();
}
else
{
i = Convert.ToInt32(cache);
}
Response.Write(i.ToString());
}
public int GenerateCache()
{
var sw = new Stopwatch();
sw.Start();
++i;
Response.Write(i+"<br>");
while (sw.ElapsedMilliseconds < 1000 * 120) { }
Cache.Insert("cache", i.ToString());
return i;
}
}
Because you have a bug by declaring again the i on the PageLoad
protected void Page_Load(object sender, EventArgs e)
{
int i; // <----- here, this is probably bug and you must remove this line
also you need some kind of locking to avoid multiple calls at the same moment, even tho you saved by the lock of the page session for the moment.

Does timer affect other processes?

I am using timer and thread in class file and then calling this class file in global file. If timer calls then will it affect other processes of the website ???
My code is as below :
public void Scheduler_Start()
{
Thread thread = new Thread(new ThreadStart(ThreadFunc));
thread.IsBackground = true;
thread.Name = "ThreadFunc";
thread.Start();
}
protected void ThreadFunc()
{
System.Timers.Timer t = new System.Timers.Timer();
t.Interval = 24 * 60 * 60 * 1000;
t.Enabled = true;
t.AutoReset = true;
t.Start();
t.Elapsed += new System.Timers.ElapsedEventHandler(TimerWorker);
}
protected void TimerWorker(object sender, System.Timers.ElapsedEventArgs e)
{
// Code here...
}
Gloabl File :
void Application_Start(object sender, EventArgs e)
{
Scheduler myScheduler = new Scheduler();
myScheduler.Scheduler_Start();
}
If timer calls then will it affect other processes of the website ???
No, it will not affect other applications and processes. By the way when you wrote this recurring background processing in your web application were you aware of the dangers?

how to create scheduler application for mail in asp.net?

my question is, I have to send mail at specific time daily, is there any way to do this in asp.net ?
give me appropriate suggestions.
Note : i don't want to run windows application or windows scheduler.
code which i used in global.asax
private static CacheItemRemovedCallback OnCacheRemove = null;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AddTask("Remoder", 5);
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
public void Remoder()
{
HttpContext.Current.Response.Write("Hello Start");
}
Have a look at http://quartznet.sourceforge.net/
I think this technique from Jeff Himself is what you need:
https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
I tried the hack ... turning your application global.asax into a virtual scheduler. It was my choice, because my server admin department simply refuses (because they don't understand) to do it with windows, or a database job.
If I had my druthers though, I'd use a database "job", since sql-server can send mail directly. Not sure what db you're using, but if you have the necessary access, I'd recommend looking for a solution like that, instead of trying to fool your asp.net app.

Action after 10 minutes ASP.NET

In ASP.NET C# how to make a action after 10 minutes? It must be without the use of browser... Obviously an server side action...
You could set a timer in Global.asax to fire every 10 minutes:
private static Timer m_MailUpdateTimer;
protected void Application_Start(object sender, EventArgs e)
{
m_MailUpdateTimer = new Timer(MailUpdateTimer_Check, null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
}
private static void MailUpdateTimer_Check(object state)
{
// Do something here.
}
protected void Application_End(object sender, EventArgs e)
{
if (m_MailUpdateTimer != null)
m_MailUpdateTimer.Dispose();
}
Of course, this will only fire if the web application is active, so if there is no usage for a while and IIS unloads it from memory, then the timer will not fire.
You may also want to consider using a Windows service or a scheduled job, which might be better suited for your needs.

Fire just once a Thread in Asp.net WebSite Global.asax

I've a legacy application using Asp.Net WebSite (winforms...) and I need run a background thread that collect periodically some files.
But this thread must run just one time!
My problem start when I put a method in Application_Start:
void Application_Start(object sender, EventArgs e) {
SetConnection();
SetNHibernate();
SetNinject();
SetExportThread();
}
So I start my application on Visual Studio and three threads start to run.
I need some singleton? or something?
Try creating a static method and variable:
private static bool _inited = false;
private static object _locker = new object();
private static void Init()
{
if (!_inited)
{
lock(_locker)
{
// Have to check again because the first check wasn't thread safe
if (!_inited)
{
SetConnection();
SetNHibernate();
SetNinject();
SetExportThread();
_inited = true;
}
}
}
}
void Application_Start(object sender, EventArgs e)
{
Init();
}

Resources