Does timer affect other processes? - asp.net

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?

Related

Loop through timer in c#

I want execute a piece of code within a time period and want to exit after a certin time period.
I have a piece of code below which goes into infinite loop.
protected void Page_Load(object sender, EventArgs e)
{
System.Timers.Timer aTimer;
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 2000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
try
{
Console.WriteLine("Page loaded", null);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message, "Default");
}
}
I want to run this code for 1 minute.
So how to stop the execution and get out of the loop after 50 or 60 seconds.
Plz help me in this.
Try this:
Stopwatch stopwatch = new Stopwatch();
// Begin timing
stopwatch.Start();
// Do something
for (int i = 0; i < 1000; i++)
{
Thread.Sleep(60);
}
// Stop timing
stopwatch.Stop();
Can you set a boolean m_SetEnabled = true in your existing OnElapsedTime event and then add if(m_SetEnabled) { m_SetEnabled = false; return; } to just ignore the single event that gets fired.
Use the Timer class.
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.71).aspx
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed+=new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval=5000;
aTimer.Enabled=true;
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
// Specify what you want to happen when the Elapsed event is raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("Hello World!");
}
The Elapsed event will be raised every X amount of seconds, specified by the Interval property on the Timer object. It will call the Event Handler method you specify, in the example above it is OnTimedEvent

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.

Selenium-RC loading empty frame

As stated above, I am running out an automated test on a website.
I am use selenium RC to do that but I'm just not sure why I am unable to open the website (actually i did open it), but its content is not showing.
There are just a few empty frame boxes.
This originally had too much code so I'm adding some more.
Anyone know why? Thank you.
Here is my code (unrelated code removed):
private ISelenium selenium;
private StringBuilder verificationErrors;
private Process worKer = new Process();
private string
serverHost = "localhost",
browserString = #"*iexploreproxy",
startUpURL = "";
private int
portNumber = 4444;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "CNY")
{
startUpURL = "http://malaysia.yahoo.com/";
}
}
private void btnStartServer_Click(object sender, EventArgs e)
{
worKer.StartInfo.FileName = #"C:\LjT\SeleniumServer.bat";
worKer.Start();
}
private void WakeUpSElenium()
{
selenium = new DefaultSelenium(serverHost, portNumber, browserString, startUpURL);
selenium.Start();
verificationErrors = new StringBuilder();
}
private void ToDoList()
{
selenium.Open("/");
//selenium.SelectFrame("iframe_content");
selenium.Type("id=txtFirstName", "1");
selenium.Click("id=rbtnGender_0");
}
private void btnTest_Click(object sender, EventArgs e)
{
try
{
WakeUpSElenium();
ToDoList();
}
catch
{}
}
You are not navigating anywhere, i.e this code here, will not navigate to any page at all:
selenium.Open("/");
I assume you meant to make it this:
selenium.Open(startUpURL); // this is the value from the combobox.

Wants to call a function after time interval in 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)
{
}

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