I have this code behind in asp.net page:
protected void LoadFile(object sender, EventArgs e)
{
try
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler( RemoveFile );
myTimer.Interval = 60000;
myTimer.Start();
}
catch (Exception)
{
}
}
private void RemoveFile(object source, ElapsedEventArgs e)
{
string path = UniquePath();
File.Delete(path);
}
When LoadFile event handler fired the RemoveFile function fired after 60 sec(as defined in this row myTimer.Interval = 60000), if LoadFile fired again after 40 seconds the RemoveFile will fire in 20 seconds.
My question is how to make the RemoveFile function to be activated after 60 seconds from last call of the LoadFile event hanlder?
May be you could use
myTimer.Stop(); just after Timer myTimer = new Timer();
I would use Microsoft's Reactive Extensions (NuGet "Rx-Main") for this. The code becomes:
private SerialDisposable _serialDisposable = new SerialDisposable();
protected void LoadFile(object sender, EventArgs e)
{
_serialDisposable.Disposable =
Observable
.Interval(TimeSpan.FromSeconds(60.0))
.Subscribe(n =>
{
string path = UniquePath();
File.Delete(path)
});
}
Now, observables are like events and calling .Subscribe is like attaching to an event. The .Subscribe call returns an IDisposable which you can use to detach from the observable by calling .Dispose(). The SerialDisposable object is a special disposable class provided by Rx that lets you assign new disposables and automatically dispose any previously assigned disposables. This automatically resets the timer every time LoadFile is run.
This is just one use of Rx - it has so many more uses and is very powerful and worth learning.
Related
My application requires 22 separate timers. So I created a timer list like so.
List<Timer> myTimers = new List<Timer>();
for(int i = 0; i < 22; i++)
{
myTimers.Add(new Timer());
}
Then I set settings for the timers
foreach(var timer in myTimers)
{
timer.Elapsed += new ElapsedEventHandler(ElapsedTimerEventHandler);
timer.Interval = 10000;
timer.Enabled = false;
}
I then created a method to start the timer.
public void SetTimerMethod(int timerId)
{
var timer = myTimers.ElementAt(timerId);
timer.Enabled = true;
}
My question is in my Event Handler how do I find out which timer called the event handler. I tried using indexOf(myTimers); on the source object passed to the event handler but that does not work. The reason I need the index position is because I need to update a field in a list of class objects based upon which timer expired. Any help would be appreciated.
I am new to this so I am open to doing this in a different way if this is a bad way of doing what I want to.
If you really want the index and not just the timer, you could use IndexOf
private ElapsedEventHandler(object sender, ElapsedEventArgs e)
{
var index = myTimers.IndexOf((Timer)sender);
}
If every timer has the same interval and is started the same time what is the point of having many of them versus one? Anyway, the sender property of the Elapsed event is the source timer (see also the docs:
private void ElapsedTimerEventHandler(Object source, System.Timers.ElapsedEventArgs e)
{
var timerCausingThisEvent = (Timer)source; // source is the timer
var timerInList = myTimers.First(t => t == timerCausingThisEvent);
var index = myTimers.IndexOf(timerCausingThisEvent);
}
So I want to perform some button clicks say every in 10 second, and here is my code:
using System;
using System.Timers;
public class Main : Form {
public Main() {
InitializeComponent();
// add timer to do button clicking every 10 seconds
double elapse = 10000;
System.Timers.Timer timer2 = new Time(elapse);
timer2.Elapsed += new ElapsedEventHandler(ResetEvent);
timer2.AutoReset = true;
timer2.Start();
}
private void ResetEvent(object source, ElapsedEventArgs e) {
try {
Refresh_Button.PerformClick();
Process_Button.PerformClick();
} catch { }
}
private void Refresh_Button_Click(object sender, EventArgs e) {
// some code
}
private void Process_Button_Click(object sender, EventArgs e) {
// some code
}
}
However, it doesn't work. Is there anything wrong with the code? How can I make it works?
The problem is accessing UI thread illegally in Elapsed event of System.Timers.Timer.
You are calling Refresh_Button.PerformClick(); in Elapsed event of timer that cause an cross thread exception that you are hiding it.
To access UI thtread and call PerformClick() method of Refresh_Button:
Refresh_Button.Invoke(new Action(() => { Refresh_Button.PerformClick(); }));
Also you can use System.Windows.Forms.Timer instead and handle Tick event and call Refresh_Button.PerformClick(); manually.
Note:
Don't hide exceptions. If you hide exceptions, such problems will hide and finding them will be really hard.
It's better to put the logic a method and instead of calling PerformClick, call that method.
If you don't need a different thread, System.Windows.Forms.Timer whould be enough.
I have included the System.Timers package, but when I type:
Timer.Elapsed; //its not working, the property elapsed is just not there.
I remember it was there in VB.NET. Why doesn't this work?
It's not a property. It's an event.
So you gotta provide an event handler that will execute every time the timer ticks. Something like this:
public void CreateTimer()
{
var timer = new System.Timers.Timer(1000); // fire every 1 second
timer.Elapsed += HandleTimerElapsed;
}
public void HandleTimerElapsed(object sender, ElapsedEventArgs e)
{
// do whatever it is that you need to do on a timer
}
Microsofts example.
http://msdn.microsoft.com/en-us/library/system.timers.timer.elapsed.aspx
Elapsed is an event and therefore requires an eventhandler.
using System;
using System.Timers;
public class Timer1
{
private static System.Timers.Timer aTimer;
public static void Main()
{
// Create a timer with a ten second interval.
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 2000;
aTimer.Enabled = true;
Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
}
// Specify what you want to happen when the Elapsed event is
// raised.
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
}
/* This code example produces output similar to the following:
Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
*/
The previous answers here are all correct, however with .net 6 / VS2022 now out and about nullability is big deal, and all the above answers will throw compiler warning CS8622.
The solution to this is to simply mark the source object as nullable in your callback function's parameters, like such:
...
var timer = new System.Timers.Timer(2000); // every 2000ms
timer.Elapsed += TimerElapsedHandler;
...
public void TimerElapsedHandler(object? source, ElapsedEventArgs e)
{
//Your Handling Code Here
}
You need an event handler, then after Enabling while assigning event handler and stop in your handler a condition
Timer = new System.Timers.Timer();
Timer.Elapsed += new System.Timers.ElapsedEventHandler(PageLoaded);
Timer.Interval = 3000;
Timer.Enabled = true;
...................
public void PageLoaded(object source, System.Timers.ElapsedEventArgs e)
{
// Do what ever here
if (StopCondition)Timer.Enabled = false;
}
What should be straight forward is not here and I couldnt find a way yet in spite of reading a lot.
I have a button which executes a time consuming function. So on clicking the button should show time elapsed in milliseconds in a label with an interval of 500 ms. And when the desired result is achieved I want the timer to stop. I dont just need the final time (the total time consumed) in a label, but the label should dynamically show the time being elapsed. My code would be:
private void btnHistory_Click(object sender, EventArgs e)
{
Class1 c = new Class1();
c.StartClock(ref label12);
Utility.PopulateHistory(dgvRecords_history, _util); //time consuming function
c.StopClock();
}
And in Class1 I write this:
internal void StartClock(ref Label l)
{
Timer t = new Timer();
t.Interval = 500;
t.Enabled = true;
t.Tag = l;
t.Tick += new EventHandler(t_Tick);
t.Start();
}
int i;
bool stop;
void t_Tick(object sender, EventArgs e)
{
if (stop)
{
((Timer)sender).Stop();
return;
}
((Label)((Timer)sender).Tag).Text = (++i).ToString();
}
internal void StopClock()
{
i = 0;
stop = true;
}
What happens is, the t_Tick event is fired only after the complete code under button event is fired. That is the tick event is fired after it goes through the StopClock function! I got no idea why on earth it should be that!
2 questions basically:
How can my requirement be achieved in the right way to handle these? I know I should use other built in classes to evaluate performance, but this is just for display purpose. For this, what is the ideal approach?
Why is my code not working?
EDIT: I have used here System.Windows.Forms Timer here, but the result is not any different with System.Timers Timer
The problem is that your long-running task is also running on the UI thread. So the timer can't fire and update the UI, since the thread is busy handling the long-running task.
Instead, you should use a BackgroundWorker to handle the long-running task.
In code:
private void btnHistory_Click(object sender, EventArgs e)
{
Class1 c = new Class1(ref label12);
c.StartClock();
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += (s, e) =>
{
// time consuming function
Utility.PopulateHistory(dgvRecords_history, _util);
};
backgroundWorker.RunWorkerCompleted += (s, e) =>
{
c.StopClock();
};
backgroundWorker.RunWorkerAsync();
}
As ChrisWue noted, since you now have the long-running task in a separate thread, it needs to invoke any access to the UI controls on the UI thread.
If your long-running task just needs some data from the UI to start, you can pass that data as parameter of RunWorkerAsync(). If you need to output some result data to the UI, you can do that in the handler of the RunWorkerCompleted event. If you occasionally need to update the UI as progress is being made, you can do that in the handler of the ProgressChanged event, calling ReportProgress() in your DoWork handler.
If none of the above are needed, you could use the ThreadPool, as in StaWho's answer.
Your time consuming function is blocking the main thread. You can use BackgroundWorker or below trick:
public Form1()
{
InitializeComponent();
t.Tick +=new EventHandler(t_Tick);
t.Interval = 500;
}
int timeElapsed = 0;
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
private void button1_Click(object sender, EventArgs e)
{
t.Start();
ThreadPool.QueueUserWorkItem((x) =>
{
TimeConsumingFunction();
});
}
void TimeConsumingFunction()
{
Thread.Sleep(10000);
t.Stop();
}
void t_Tick(object sender, EventArgs e)
{
timeElapsed += t.Interval;
label1.Text = timeElapsed.ToString();
}
Add the timer to the Components collection of the form. Or store the timer in a field in the class.
The timer is garbage collected because it is not longer reachable when your method returns.
I don't know about your long running code, but out should new run on a separate thread, or make calls to Application.DoEvents
(And remove the ref in your code, it is not used).
#Dainel Rose's answer worked for me perfectly, but only if invalid cross thread operation is handled. I could do so like:
private void btnHistory_Click(object sender, EventArgs e)
{
Class1 c = new Class1(ref label12);
c.StartClock();
var backgroundWorker = new BackgroundWorker();
backgroundWorker.DoWork += ((s, e) =>
{
// time consuming function
Utility.PopulateHistory(dgvRecords_history, _util);
});
backgroundWorker.RunWorkerCompleted += ((s, e) =>
{
c.StopClock();
});
backgroundWorker.RunWorkerAsync();
}
And in the Utility class where the time consuming function runs,
internal static void PopulateHistory(DataGridView dgv, Utility util)
{
SetDataGridView_History(dgv, util);
}
delegate void UpdateDataGridView_History(DataGridView dgv, Utility util);
static void SetDataGridView_History(DataGridView dgv, Utility util)
{
if (dgv.InvokeRequired)
{
UpdateDataGridView_History updaterDelegate = new UpdateDataGridView_History(SetDataGridView_History);
((Form)util._w).Invoke(updaterDelegate, new object[] { dgv, util });
}
else
//code that utilizes UI thread (long running process in my case)
}
Thanks all who helped. I'm marking Daniel's answer..
private void aMethod()
{
aTimer = new System.Timers.Timer(3000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimerEvent);
aTimer.Enabled = true;
aTimer.Start();
}
private void button4_Click(object sender, RoutedEventArgs e)
{
fileEntries = Directory.GetFiles(#"C:\Users\John\Documents\Visual Studio 2010\Projects\ComeOn\ComeOn\bin\Debug\come");
aMethod();
index = 0;
}
private void OnTimerEvent(Object sender, ElapsedEventArgs e)
{
Bitmap LogoImg = new Bitmap(fileEntries[index]);
LogoImg.MakeTransparent(LogoImg.GetPixel(1, 1));
this.Dispatcher.Invoke(
new Action(() => image1.Source = GetBitmapSource(LogoImg)));
index++;
}
The length of fileEntries is 3. I created a timer which will start on 3 seconds. First it will execute image1.Source = GetBitmapSource(LogoImg)//for fileEntries[0] for 3 seconds, then for fileEntries[1] for 3 seconds and in the end fileEntries[2] for 3 seconds.
But, my program does this:
Start the timer, run fileEntries[0], fileEntries[1] and fileEntries[2] for 0.05 seconds, then wait 3 seconds, then start again. Why is this?
How often did you click that button?
Every time you press the button, a new event handler will be hooked to the timer. You never unsubscribe the event handler.
You should either prevent the button from being clicked while you are performing the required work, or you should unsubscribe before subscribing again.
As Hans Passant states in his comment, you should probably also look into using a BackgroundWorker.
You shouldn't do
aTimer = new System.Timers.Timer(3000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimerEvent);
aTimer.Enabled = true;
aTimer.Start();
more than once. Do it in Form_Load event, or in constructor. in your OnTimerEvent event, prevent your code from being executed when files aren't initialized, for example
int index = -1;
private void OnTimerEvent(Object sender, ElapsedEventArgs e)
{
if(index != -1)
{
Bitmap LogoImg = new Bitmap(fileEntries[index]);
LogoImg.MakeTransparent(LogoImg.GetPixel(1, 1));
this.Dispatcher.Invoke(
new Action(() => image1.Source = GetBitmapSource(LogoImg)));
index++;
}
if (index == 3) // when all 3 were loaded, reset index. You can also stop the timer if you won't be loading files the second time
{
index=-1;
}
}
Or you should unsuscribe before you add new event handler. But keeping track of how many event handlers are added to an event is tricky (or I should say I havn't found a way to do it yet).
As #Steven Jeuris said, when an event handler is added to an event, it is literaly ADDED, to event handlers LIST. So every time when your timer elapses every event handler on the list is executed, which means if there are 3 event handlers added (as in your case) the event handler method will execute 3 times.