Click button automatically at regular intervals - c#

I have a web application that generates enlarged images of smaller images uploaded by other team and stops if no image is left in uploaded folder.
A button needs to be clicked to start generating image and restart every time it finishes.
Can this click event be fired automatically at regular interval say at 15 minutes or better still just as the application stops.
A similar question was asked on earlier thread How to write C# Scheduler .
private void OnTimerTick()
{
if (DateTime.Now.Minutes % 15 == 0)
{
// Do something
}
}
Where should I place timer to call below code ?
protected void btnImgGenerate_click()
{
// Application code
}
Is there a way to check whether a web application in asp.net has stopped executing and then restart it automatically?
Using timer, I can schedule application to start and stop at specific time of day and keep application running throughout specified duration of day using second method.

You can add code to initialize timer from the System.Threading namespace in Global.asax file and configure it to execute some functionality with desired periodicy. But if your application crashes you'll get following issue:
Is there a way to check whether a web application in asp.net has
stopped executing and then restart it automatically ?
Using timer , I can schedule application to start and stop at specific
time of day and keep application running throughout specified duration
of day using second method .
Nope. You can't start web appliccation from itself if it stopped by some reasons and no new requests comes. So in my opinion for your purpose better suited a windows service.

A jQuery implementation for clicking a button after 15 minutes (which will cause a postback and trigger your event) is:
$(document).ready(function(){
setTimeout(function(){$('btnImgGenerate').click();},900000);
});

Have a Button click event that starts the timer-
protected void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
and in the timer Tick event, you can do something like this-
protected void timer1_Tick(object sender, EventArgs e)
{
timer1.enabled=false;
//do your coding
timer1.enabled= true;
}
So once you click the button the timer starts, does all the operations and at the end restarts automatically, after you set timer1.enabled to true.
If you are looking for automating this without the click event, you can use the javascript setInterval() Method-
See the example here-
http://www.w3schools.com/jsref/met_win_setinterval.asp

Related

Control.ValueChanged fire with time offset

I have some C# controls like NumericUpDown or TextBox and want to fire an event with an offset of some seconds. In the past, I have accomplished this behaviour with a Timer. The code which I am working on uses a BackgroundWorker to accomplish this. On some other places, I found normal Threads to build this behaviour.
The reason why one might want an offset is, for example, a time-consuming method which is executed after each ValueChange of a NumericUpDown. If a user clicks several times on the down arrow only the last Click should be of importance because this is the value which the user wanted in the end.
The way I used to handle this looks as follows
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
/*
* Designer Code
this.eventOffsetTimer.Interval = 500;
this.eventOffsetTimer.Tick += new System.EventHandler(this.eventOffsetTimer_Tick);
*/
eventOffsetTimer.Stop();
eventOffsetTimer.Start();
}
private void eventOffsetTimer_Tick(object sender, EventArgs e)
{
eventOffsetTimer.Stop();
//Time consuming stuff...
MessageBox.Show(numericUpDown1.Value.ToString());
}
I was wondering what the best practice is to accomplish an offset of some seconds before the event is fired. Is there a built-in way which Microsoft encourages to use? Starting and stopping a Timer is a simple thing to do but it seems there could be a Microsoft encouraged method.
You should use .Restart(). It first releases Timer then Starts it again.
Tips: You do not need to use .Stop() with .Restart()

How to trigger an event after a timer runs down if the user does not click a control

I'm new to stackoverflow so please forgive any protocol transgressions.
I'm trying to put together a 'times tables' practice program for my grand-daughter using c#. I'm very new to programming (just past the 'Hello World' stage). I have a 'greetings' form set up where the user (my grand-daughter, usually but not always) is asked for her name and, on pressing the Enter key, is greeted with a Textbox containing a friendly message and a request to click on the message if she'd like to 'play'. If she clicks, she is taken to a combo-box where she can choose a 'game'. I would like to attach a timer to that Textbox which shows another Textbox if she doesn't click within, say, 5 seconds. I have got as far as attaching a Timer to the form and enabling it, but cannot work out what to do next. This is the code I have so far:
private void playerOneNameTextbox_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
greetingsTextbox.Visible = true;
greetingsTextbox.Text = "Hi, " + playerOneNameTextbox.Text +
". It's good to see you. Click here if you'd like to play with us";
timer1.Enabled = true;
/*If there is no click within five seconds,
*another textbox should become visible offering another chance to click.
*/
}
private void greetingsTextbox_Click(object sender, EventArgs e)
{
youHaveClickedLabel.Visible = true;
chooseGameComboBox.Visible = true;
}
Apologies if this question is too wordy - please let me know if it is and I'll try to be more succinct next time. Many thanks.
You haven't included any details on how the timer has been set up (or what type of Timer it is, as I believe there are several).
I'm going with the assumption that you've designed the form through Visual Studio and have dragged in a Timer from the Toolbox.
This being the case, it should be a System.Windows.Forms.Timer timer object.
This type of timer has a Tick event which you will need to associate to an event handler. In VS you would do this by right-clicking on the timer1 object (located at the bottom of the design window) and selecting Properties. In the resulting properties window, click on the Events button (which looks like a lightning bolt). In here you'll have the one event "Tick".
Double-click on this and it'll automatically create the event handler stub code which you can then modify to do what you require with it.
You may wish to change the value of Interval on the timer1 object as this is the length of time (in milliseconds) which would elapse before it fires the Tick event, so in your case you'd want it to read 5000.

Application state to record time spent using application asp.net

Ok, I have an application and I want to record how long the user spent using the application. At the moment I'm recording everything using session and cookies etc. But how do I start a timer without display it and then end it when the user presses a button to say they're finished. Here's some code. Thanks!
// get the time spent
DateTime timeSpent = Convert.ToDateTime(Application["timeSpent"])
protected void Application_Start(object sender, EventArgs e)
{
Application["timeSpent"] = 0;
}
How do I trigger this timer without the user knowing about it etc. Thanks!

How to detect my application is idle in c#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if an application is idle for a time period and lock it
Suppose I have a Windows product developed with C#. Now a requirement comes that if that application running and idle and when user try to interact with that application again then a login screen come. How to detect that my applications is idle when it is running? Please guide me to complete the job.
Add a timer control to your
application.
Subscribe to mouseover and keydown
events - when they fire, reset the
timer.
When the timer fires (ie mouse
hasn't moved and key's haven't been
pressed for x amount of time), lock
the screen / prompt for login.
You can use Control.LostFocus to record time when user navigated away then use Control.GotFocus to check how much time has passed to determine whether or not they need to log in.
Here go my simple solution:
Point cursorPoint;
int minutesIdle=0;
private bool isIdle(int minutes)
{
return minutesIdle >= minutes;
}
private void idleTimer_Tick(object sender, EventArgs e)
{
if (Cursor.Position != cursorPoint)
{
// The mouse moved since last check
minutesIdle = 0;
}
else
{
// Mouse still stoped
minutesIdle++;
}
// Save current position
cursorPoint = Cursor.Position;
}
You can setup a timer running on 60000 interval. By this way you will just know how many minutes the user don't move the mice. You can also call "isIdle" on the Tick event itself to check on each interval.
Capture the leave event of the Form class object to know it has lost focus.

C# DateTimePicker stuck in loop

I have a datetimepicker in C#. When I click on it, it expands to show a monthly calendar, when I click the left arrow to go back a month, it changes the value and calls my event. The event includes too much code to include here but it calls several functions needless to say.
The problem I'm having is that when I click that left arrow it gets stuck in some sort of loop and keeps descending through the months and I can't stop it. One of the functions that is being called contains a Application.DoEvents() and if I comment that out it doesn't get stuck in the loop, but I need that command to update another section of the interface. Any idea why this is happening?
I can duplicate it sometimes with this code, sometimes it just does it a couple times, sometimes it gets stuck in the loop.
private void DateTimePickerValueChangedEvent(object sender, EventArgs e)
{
afunction();
}
private void afunction()
{
listView1.Clear();
panel1.Visible = true;
Application.DoEvents();
}
I also have the same problem. In my case, instead of calling DoEvents I'm updating a Crystal Report view. The only workaround I found is to update my view upon the CloseUp event instead of ValueChanged or TextChanged.
Scott, how did you finally corrected your problem ?
The DateTimePicker ValueChanged event is buggy. Per Microsoft Windows Forms Team on this page https://connect.microsoft.com/VisualStudio/feedback/details/1290685/debugging-datetimepicker-event-hangs-vs:
"The DateTimePicker control installs a mouse hook as part of its functionality, but when the debugger has the WinForms application stopped on a breakpoint, it allows the possibility of a deadlock if VS happens to get a mouse message. For now, the deadlock is unfortunately a consequence of the DateTimePicker's design. The mouse hook is installed when the drop down is clicked to display the calendar. This means that breakpoints should not be sent in any event handlers which would be called while the calendar is active. We are currently investigating whether it is possible to address this issue and we will update this thread with further information if we are able to make a fix available."
Without seeing any of the code, try these steps:
Comment out the entire event handler
to see how fast it runs with nothing
attached to it.
Uncomment lines one at a time to see
which ones are causing the most
problems.
Analyze those method calls.
...
Profit!
You could try a couple of things. Get rid of the DoEvents inside of the ChangedEvent.
Call the doevents inside of a seperate function after maybe a period of time (thread.sleep() ?).
I know doevents does cause issues but I rarely use it.
event procedure ValueChanged :
set parameter in sender.tag
enableTimer and execute parameter using sender.tag
example:
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateTimePicker ThisSender = (DateTimePicker)sender;
Timer.Tag = ThisSender.Name.ToString() + "=" + ThisSender.Value;
Timer.Enabled = true;
}

Categories