Visual Studio 2008, Timer, c# - c#

If i want my application to do something every 2hr (eg. pop up a message), how do i do that?
Do i program that set of code under onLoad() or somewhere else?

Assuming WinForms.
You should use Windows Timer Class
Drag and drop timer component to your form.
Set interval to 7200000 (2 * 60 * 60 * 1000) milliseconds.
Subscribe to Tick event (the only event that this component has).
private void timer1_Tick(object sender, EventArgs e)
{
MessageBox.Show("Example");
}
The code inside of timer will be triggered every 2 hours, if UI thread is not blocked.

Check the Timer Control and event Tick
Timer.Tick - MSDN

Use the Timer class and set it up when the application starts.

Related

Is there a way to update a windows form in a loop?

My goal is to make a desktop pet. I've already programmed a lot of logic that executes in a while loop and updates every iteration. To display the creature I'm looking to use windows forms, but that has brought up a dilemma.
I want to be able to execute the logic, and then update the window in the same loop (process events and redraw), without having to deal with Application.Run() or multi threading. As an example, and as someone who's come from python, using tkinter it's possible to call the update() method on a window in a loop, which is essentially the same as calling mainloop() once, except it doesn't block the program.
Do forms offer any similar functionality?
As Scott Chamberlain mentioned, you should use a timer to run your 'loop'. Winforms is event based so adding an infinite loop will freeze the program since events are blocked.
This code illustrates how to use a timer. I added a picture box to the form and it moves across the screen as the timer fires.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Timer tmr = new Timer();
tmr.Interval = 50; // milliseconds
tmr.Tick += Tmr_Tick; // set handler
tmr.Start();
}
private void Tmr_Tick(object sender, EventArgs e) //run this logic each timer tick
{
pictureBox1.Left += 1; // move image across screen, picture box is control so no repaint needed
}
}

Update textbox during timer_tick

I have read many answers on this question, and yet I still cannot get this to work. I have a simple C# WinForms app with a timer control. When the timer fires, I have some code do some processing. I want to update a textbox with status during this processing. But the textbox never gets updated until the eventhandler finishes. Please tell me how I can get the textbox to update during the processing.
Here is my code:
My Form:
public Form1()
{
InitializeComponent();
timer1.Interval = 60000;
timer1.Tick += new EventHandler(CheckStatus);
timer1.Start();
}
private void CheckStatus(object Sender, EventArgs e)
{
// Set the caption to the current time.
textBox1.AppendText(DateTime.Now.ToString() + Environment.NewLine);
ProcessStatus();
}
private void ProcessStatus()
{
textBox1.AppendText("Now updated" + Environment.NewLine);
}
If I step through my code, the textbox is not updated until I step out of CheckStatus. (I'm using Visual Studio 2017)
I have tried several things like what is found here: StackOverflow
When the timer ticks it's firing on the GUI thread. While the GUI thread is busy processing (I assume whatever you're doing takes a long time) all other GUI updates will pause.
You can run textBox.Update() to force the update at that point, but that's not considered a best practice.
Instead, you should run your process on a background thread. One option is BackgroundWorker and use the ProgressChanged event to show your updates in your GUI.

continue of timer after closing the form

var startTime = DateTime.Now;
var timer = new Timer() { Interval = 1000 };
timer.Tick += (obj, args) =>
label14.Text =
(TimeSpan.FromMinutes(Convert.ToDouble( comboBox3.Text)*60) - (DateTime.Now - startTime))
.ToString("hh\\:mm\\:ss");
MessageBox.Show("Timer started");
timer.Enabled = true;
im strating a timer in a specific form when pressing a button, i want the timer to continue counting time after closing the form in order to give me a notification.
but when closing the form the timer stops,
any ideas?
You can't "Close" the form and have the Timer object still work (if you're calling the timer on the main form). (That's like saying I can't click the button and have a function run after closing the form). However, on the OnFormClosing argument, you could have the form.Visible = false and cancel the actual closing of the form. See here: Timer doesn't fire after Form closed
If you want to truly close the form and have it run, one option could be to look at possibly creating a Task Scheduler object that would complete the task at a certain time.
A quick hit: call "Hide()" instead - that will keep the timer running.
Now that's out the way, You can still make your timer static - ideally put it in a completely separate class, or, if you insist, make it static on your main form. Then have your notification code sit on your main form.
You can still add a listener to the Tick event on that specific form and just remember to remove it when you hide the form (i.e. Tick -= your_label_event_handler)

Run C# file automatically

Hello all of you on stack overflow.
I have Asp.net web application which picks up image from a folder , resize them and dump in other folder. For this i have to click on a button to pick image an work on it very often.
Is there a way to make application run automatically by picking image from folder and working on it and then dump in other folder.
Setting can be like start application and run for five minutes and stop and then wait for 1 minute and then start and run again for 5 minute.
IMPORTANT thing is it should only stop after it has finished resizing and dumping the last image in interval.
this will save me lot of trouble because I have to click on button very frequently and disturb me .
I only want the approach and related LINKS .Lots of them . Please reply.
use a timer control to do this task
timer.Tick += new EventHandler(timer_Tick); // Everytime timer ticks, timer_Tick will be called
timer.Interval = (1000) * (1); // Timer will tick evert second
timer.Enabled = true; // Enable the timer
timer.Start();
void timer_Tick(object sender, EventArgs e)
{
do whatever you want
}

Can a c# timer call the .stop function on itself?

Whenever I try to do something like this the timer doesnt stop:
private void timer1_Tick(object sender, EventArgs e)
{
if ((addedToFriendsCounter == 4) || (followJobFinished))
{
//stop the timer
}
}
Any suggestions?
Yes, no problem. A comment can't stop a timer. Use
timer1.Stop();
or
((Timer)sender).Stop();
There's no problem stopping the timer from within the Tick event handler. What the heck is addedToFriendsCount and followJobFinished? Your error is either with one of these or the code for //stop the timer.
Yes, there is no problem stopping the timer from the Tick event. The event runs in the main thread, so there is no cross-thread problems when you access the Timer control.
You can stop the timer either by calling the Stop method or by setting the Enabled property to false.

Categories

Resources