timer.Elapsed actions not firing - c#

I have a button that I want to disabled after 4 failed submit attempts. I am using System.Timers timer to count to 15 seconds, then trying to re-enable the button after those 15 seconds.
Here's my code:
if (SessionManager.getRetries > 3)
{
Message.Text = "Too many failed attempts. Please retry in 15 seconds.";
buttonOne.Enabled = false;
HttpContext.Current.Session["RetryCount"] = 0;
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.AutoReset = false;
myTimer.Interval = 15000;
myTimer.Enabled = true;
myTimer.Start();
myTimer.Elapsed += myTimer_Elapsed;
}
}
void myTimer_Elapsed(object sender, ElapsedEventArgs e)
{
buttonOne.Enabled = true;
}
So far my program will correctly count the # of submissions tried (SessionManager.getRetries). After 4 failed attempts, it will enter this if statement. It then creates the timer, sets the interval, starts it, and knows that the time elapsed. It even goes to the buttonOne.Enabled = true statement, but it doesn't actually enabled my button. (This was observed through the use of breakpoints in visual studio).
Is there something I am missing here? Maybe elapsed events don't work the way I want them to?
Any help would be appreciated.

Timer event should be firing, but it has no way to enable button on HTML page as request is completed by that point.
You should move timer to JavaScript on the page. You still need to keep logic that verifies that retries are not more frequent as it is very easy to bypass JavaScript or any other client side verification.

As posted earlier, the problem was not that the events weren't firing, it was that there was no way to show the button as enabled on the client side via the method I was trying previously because the timer only exists server-side.
BUT
I found an easier way than coming up with some sloppy Javascript :)
By using a update panel and asp timer. By placing a asp timer inside of the update panel, and setting the timer to disabled as default, I can then manipulate the timer. Then, when the timer ticks it is automatically updated via the update panel :)
Great code samples here:
ASP.NET Timer Event

Related

How do I use a asp:timer to time actions happening inside a button click event?

I have an application that allows a user to save a record to the database. When The user clicks save I want to show a label that says Saving record please wait... then after 3 seconds I want the label to say Record Saved. Finally after 3 more seconds I want to go back to the normal view. Here is hat I tried:
txtSaveReportAs.Enabled = false;
strSaveNotify = "lblWarning";
lblSaveAs.Text = "Saving Your Data. Please Wait... ";
System.Windows.Forms.Timer Timer1 = new System.Windows.Forms.Timer();
Timer1.Interval = 3000;
Timer1.Start();
strSaveNotify = "lblSuccess";
lblSaveAs.Text = "Data Saved Successfully";
Timer1.Stop();
Timer1.Interval = 3000;
Timer1.Start();
txtSaveReportAs.Enabled = true;
txtSaveReportAs.Text = string.Empty;
When I do this it cancels out my sqlCommand so txtSaveReporAs just does not become disabled. Also, strSaveNotify and lblSaveReport as does not change and no info gets saved to the database. How is this done?
You are using the Timer like a Sleep, which halts execution within the current thread - not that I am suggesting you replace it with a Sleep, because they should only be used in limited circumstances and this is not one.
For the Timer to work it needs an Tick event handler added to it. The event handler gets triggered when when the timer has completed the 3 seconds. You don't call Stop() in the same set of code, because this will turn it off immediately after you have turned it on. Processing doesn't stop and wait where you have Timer1.Start(), it keeps going.
You need to look up the specifications for the Timer.
http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.tick(v=vs.110).aspx

Timer strange behaviour

I have a Windows.Forms.Timer used in a form that works if I set its Enabled property to true in the properties window, and that is the only time it works. If I leave it disabled then enable it only when I need to, it doesn't tick.
private void btnRename_Click(object sender, EventArgs e)
{
timerUpdateProgress.Enabled = true;
timerUpdateProgress.Start();
pbProgress.Maximum = clbFiles.CheckedItems.Count;
var renameTask = Task.Factory.StartNew(() => doRename(true, tbCurrentDirectory.Text, clbFiles.CheckedItems, rules));
if(renameTask.Result.Count > 0)
{
timerUpdateProgress.Enabled = false;
new ExceptionsWindow(renameTask.Result).ShowDialog();
}
timerUpdateProgress.Enabled = false;
loadFiles(tbCurrentDirectory.Text);
}
private void timerUpdateProgress_Tick(object sender, EventArgs e)
{
pbProgress.Value = progress; //I have a breakpoint on this line
}
All I'm trying to do is display the progress of an operation with a progress bar. Enabling and starting the timer does nothing, the tick never happens. Why is this happening?
Update: stepping through the code after adding a sleep of 2000ms after enabling and starting the timer shows it still does not tick (I have a breakpoint in the tick handler).
More clarification: When the timer is enabled in the property window, the tick handler is always being called, and that's without calling Start(). My breakpoint is triggered all the time just by enabling the timer. I should also note that I'm using .NET 4.
EDIT:
After getting an idea from the comments - it seems you do not need the timer at all.
You can update the progress bar Value as and when the value of progress is updated.
That way the transition from 0 - 100% might also seem smoother.
According to me you need disable the timer.
Just use
timer.Stop();
And then timer.Start() when needed again.
Additionally, if you have disabled timer progress, then you also must do
timerUpdateProgress.Enabled = true
How long did "Task.Factory.StartNew" take ? (If this is syncronous)
If above lambda expression is asyncronous, I see whether renameTask.Result.Count is bigger than 0 or not, progress bar = false...

Timer/Refresh functionality for text box

In my application, I have two text boxes accompanied with two labels: "Connected" and "Not Connected". As seen in my code, if a connection is established, the "Connected" text box will fill with Green, indicating a network connection. It will be red if not.
The functionality of connection detection is working just fine, however, I have to re-open the application for it to detect the change. I am looking for a way to refresh the application every 5-10 seconds or so automatically to detect any change in connectivity. I don't want to flush out the contents of any other field or box, just the color text boxes. A soft polling loop so to speak. How would I go about doing this using the Timer method. Should I create a new thread in which to run the timer and refresh the box?
Thanks.
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() == false)
{
noConnect.Select(); //if not connected, turn box red
noConnect.BackColor = Color.Red;
}
else
{
netConnect.Select(); // if connected, turn box green
netConnect.BackColor = Color.Lime;
}
//need to refresh box/application without losing other box/field contents
//in order to constantly check connectivity around 5-10 seconds or so
//constantly check connectivity
Something like this would work
public Form1()
{
InitializeComponent();
var timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 10000; //10 seconds
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
if (your_function_call())
{
netConnect.BackColor = Color.Green;
}
else
netConnect.BackColor = Color.Red;
}
The timer_Tick would be repeatedly called every interval and you can poll your status and update controls. Because the timer call back is called in the UI-thread you can update any UI elements.
From Timer Class
A Timer is used to raise an event at user-defined intervals. This
Windows timer is designed for a single-threaded environment where UI
threads are used to perform processing. It requires that the user code
have a UI message pump available and always operate from the same
thread, or marshal the call onto another thread. When you use this
timer, use the Tick event to perform a polling operation or to display
a splash screen for a specified period of time. Whenever the Enabled
property is set to true and the Interval property is greater than
zero, the Tick event is raised at intervals based on the Interval
property setting.
This solution uses System.Windows.Forms.Timer that calls the tick on UI-thread. If you use System.Timers.Timer the callback won't be on UI-thread.
just create the timer. it well run on his own thread without you doing any thing else.
You can create a timer somewhere in your application
var timer = new System.Timers.Timer();
timer.Interval = 5000; // every 5 seconds
timer.Elapsed = (s, e) => {
// Your code
};
timer.Start();
Note: please be aware that your code in the Elapsed event handler can/will run on another thread!

c# chat window application problem with timer

I'm developing a chat application. For getting frequetly comming request,messages and zone request I'm using one timer and call all methods on timer.now. The problem is that when ever I click on any control in the application this gives me a late response due to the timer running. It first hangs until it completes the timer code then control click event is fire.
So, any help on how to handle this is appreciated, I also tried threading but this didn't help.
Please give me any idea if u have.
Thanks.
Use System.Timers.Timer or System.Threading.Timer instead of the Windows.Windows.Forms.Timer, and inside the Elapced event handler whenever you call methods or properties on UI controls use control.InvokeRequired and control.Invoke.
the problem with the form timer is that it perform the action on UI thread, From msdn:
Windows timer is designed for a single-threaded environment where UI
threads are used to perform processing
Edit: Here is example using System.Timers.Timer:
private System.Timers.Timer _chatTimer = new System.Timers.Timer();
public Form1()
{
InitializeComponents();
_chatTimer.Interval = 1000;//1 seconds
_chatTimer.Elapsed += OnChatTimerElapsed;
_chatTimer.AutoReset = true;
}
private void OnChatTimerElapsed(object sender, System.Timer.ElapsedEventArts e)
{
//code to perform when timer elapsed.
}
Edit2: Another thing to notice that depending on execution time on the elapsed event handler, if the time required to execute the code on it is larger than 1 second then I suggest you to set _chatTimer.AutoReset to false and only start the timer after the previous elapsed event is finished. for example check this.

Why is this C# timer code not working?

I need to be able to disable a button for 1.5 seconds at a time for an application I'm writing. An image is displayed, a user clicks a button, and then another image is displayed. I need to make sure that the user doesn't click the button again too quickly.
So, when the image is displayed, I call this function:
//when a new image is displayed, start the timer and disable the 'done' button
//for 1.5 seconds, to force people to stop pressing next so quickly
System.Timers.Timer mTimer;
void TimerStart() {
Done.IsEnabled = false;
mTimer = new System.Timers.Timer();
mTimer.Interval = 1500;
mTimer.Start();
mTimer.Elapsed += new System.Timers.ElapsedEventHandler(TimerEnd);
}
The TimerEnd code looks like:
void TimerEnd(object sender, EventArgs eArgs) {
if (sender == mTimer){
Done.IsEnabled = true;
mTimer.Stop();
}
}
The 'Done.IsEnabled' line gets hit, but the button is not reenabled and the timer doesn't stop firing. What am I doing wrong here? If it matters, this is a WPF app.
Use DispatcherTimer instead
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(someInterval);
timer.Tick += new EventHandler(someEventHandler);
timer.Start();
private void someEventHandler(Object sender, EventArgs args)
{
//some operations
//if you want this event handler executed for just once
// DispatcherTimer thisTimer = (DispatcherTimer)sender;
// thisTimer.Stop();
}
Basically you are trying to debounce the button, to prevent too quick clicks. Rather than use a timer save the previous click time in millis, if the button is clicked again within a short time ignore the next event.
The timer event is raised on a different thread. When working with the winforms controls, you need to make sure you Invoke them from the same thread where they were called.
When working with WPF there is no guarantee that updates made to UI controls on non-UI threads will work as expected. In many cases you will get an exception when you do this.
In your Timer elapsed handler you need to use the BeginInvoke/EndInvoke paradigm and put your button enabling logic in there to ensure that this code runs on the UI thread instead of Begin/End Invoke
There is a SynchnornizationContext available as well which can be accessed by calling SynchronizationContext.Current . You'll need to cache this before you make the timer call since SynchronizationContext.Current will be null in non-UI threads.
This link talks about this as well.

Categories