I have a desktop WPF application that at some point needs to do something on a timely basis. Suppose I use DispatcherTimer class. There's a "long" way:
public MainWindow()
{
// dozens lines of code
timer1.Interval = new TimeSpan(0, 0, 1);
timer1.Tick += timer1_Tick;
}
// hundreds lines of code later
DispatcherTimer timer1 = new DispatcherTimer();
void timer1_Tick(object sender, EventArgs e)
{
// do stuff
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer1.IsEnabled = checkBox1.IsChecked.Value;
}
It works ok, but I don't like that timer-related code is separated in two parts. I could re-create timer each time I start it:
DispatcherTimer timer1;
void timer1_Tick(object sender, EventArgs e)
{
// do stuff
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer1 = new DispatcherTimer();
timer1.Interval = new TimeSpan(0, 0, 1);
timer1.Tick += timer1_Tick;
timer1.IsEnabled = checkBox1.IsChecked.Value;
}
but that feels wrong.
Also DispatcherTimer have constructor that take EventHandler as an argument that I may use kinda like that:
DispatcherTimer timer1 = new DispatcherTimer(
new TimeSpan(0,0,1),
DispatcherPriority.SystemIdle,
new EventHandler(timer1_Tick),
Dispatcher.CurrentDispatcher);
void timer1_Tick(object sender, EventArgs e)
{
// do stuff
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
timer1.IsEnabled = checkBox1.IsChecked.Value;
}
I like it, it's simple and compact, but you can't create EventHandler this way, the compiler says "A field initializer cannot reference the non-static field, method or blah-blah-blah".
Is there a way to make this nice code actually work?
What's wrong with just doing it all in the constructor? That's how I normally do it:
private DispatcherTimer _timer1;
public MainWindow()
{
// dozens lines of code
_timer1 = new DispatcherTimer(TimeSpan.FromSeconds(1),
DispatcherPriority.SystemIdle,
timer1_Tick,
Dispatcher);
}
if you don't mind changing field to property (with backing field), you can do initialization in getter (only on first read):
DispatcherTimer _timer1;
DispatcherTimer timer1
{
get
{
return _timer1 ??= new DispatcherTimer
(
new TimeSpan(0, 0, 1),
DispatcherPriority.SystemIdle,
new EventHandler(timer1_Tick),
Dispatcher.CurrentDispatcher
);
}
}
void timer1_Tick(object sender, EventArgs e)
{
// do stuff
}
you could just create the timer in the constructor instead. Optionally with a local function or lambda for the handler to keep it close to the initialization.
If you follow the stylecop code style rules the code should be ordered as
Fields
Constructors
...
methods
So if there are hundreds of lines of code between the declaration and initialization of your timer there might be other design issues you need to address first.
If you have multiple constructors you should have one constructor that is guaranteed to run, and that all the other delegates some of the construction to.
Related
Here's my code
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = Convert.ToInt32(textBox5.Text);
if (checkBox1.Checked)
{
myTimer.Start();
}
else
{
myTimer.Stop();
}
}
It should stop repeating the function when unchecked, but it doesn't. What's wrong with it?
I recommend that you stop using System.Timers.Timer and start using a System.Windows.Forms.Timer component.
Begin by removing your myTimer-related code (the entire body of checkBox1_CheckedChanged will need to be replaced with code from below.)
Add a Timer component to your form using the designer and name it myTimer. This will add a System.Windows.Forms.Timer field to your form called myTimer.
Using the designer, set the Tick event handler of myTimer to DisplayTimeEvent. (Or add a new handler and replace its code with the code of your DisplayTimeEvent function.)
Then change your checkBox1_CheckedChange function to look like this:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (int.TryParse(textBox5.Text, out int interval)) {
this.myTimer.Interval = interval;
}
this.myTimer.Enabled = checkBox1.Checked;
this.textBox5.Enabled = !checkBox1.Checked;
}
I also recommend adding the following handler to textBox5 to perform the bare minimum validation so you can't crash your app by entering an interval of 0 or the empty string, or some text that is not an integer.
private void textBox5_TextChanged(object sender, EventArgs e)
{
this.checkBox1.Enabled = (int.TryParse(textBox5.Text, out int interval) && interval > 0);
}
The System.Windows.Forms.Timer's Tick handler will be called in the UI thread, meaning it will be safe to do things like update labels of your form in that handler. In contrast to that, the System.Timers.Timer will be called on a worker thread and will require that you take on some some thread-management responsibilities you likely don't want to incur, such as invoking your UI updates back to the main UI thread. See Why there are 5 Versions of Timer Classes in .NET? for more info.
Everytime checkbox1 is changed, new Timer is created. When checkbox is ticked, created timer is active and will invoke DisplayTimeEvent forever. When checkbox is unticked, you stop another Timer, which was just created.
You need to create Timer only once (probably when form is created), or when checkbox1 is changed first time:
private System.Timers.Timer myTimer;
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (myTimer == null) {
myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = Convert.ToInt32(textBox5.Text);
}
if (checkBox1.Checked)
{
myTimer.Start();
}
else
{
myTimer.Stop();
}
}
I am a new user and i wish to add a timer to hold down the program for an hour and then continues.
This is the procedure:
Issue a command through serialport e.g. high
Holds for an hour
Issue again the same command 'high'
Holds for an hour
It repeats until a button is pressed.
How should i implement the timer? Because i tried searching online and found some examples.
I included this in Form1.cs:
static System.Timers.Timer Timer1;
void button1_Click(object sender, EventArgs e)
{
Timer1 = new System.Timers.Timer(60*60*1000);
Timer1.Elapsed += new ElapsedEventHandler(TimedEvent);
Timer1.Enabled = true;
}
private void TimedEvent(Object source, ElapsedEventArgs e)
{
serialPort1.Write("high");
}
Even though this code helps to repeat the high every hour but it only does the TimedEvent after 60 minutes. I need to write into the serialport first then execute the timer. How do i amend the codes to achieve the result i want?
Edited:
I realised that this code does not work as in the timer did not hold for an hour. Instead place it in form1_load to work.
private void Form1_Load(object sender, EventArgs e)
{
Timer1 = new System.Timers.Timer(10*1000);
Timer1.Elapsed += TimedEvent;
}
Tried this (below) but the timer didn't work
static System.Windows.Forms.Timer timer4 = new System.Windows.Forms.Timer();
void button1_Click(object sender, EventArgs e)
{
Writetoserialport();
timer4.Interval = 10000; // testing on 10second interval
timer4.Enabled = true;
}
When i remove Writetoserialport() , the program runs forever.
Call TimedEvent directly for the first run:
private void Form1_Load(object sender, EventArgs e)
{
Timer1 = new System.Timers.Timer(10*1000);
Timer1.Elapsed += TimedEvent;
TimedEvent();
}
But for something that happens as rare as hourly, a Windows Service might be a better option.
This will work but will probably crash when you close the program sometimes because System.Timers.Timer triggers the TimedEvent on a ThreadPool thread. What will probably happen sometimes is when your program is closed, the SerialPort object will be disposed, but since the Timer is still executing on on another thread it will trigger the TimedEvent and try and write to the serialPort but it will crash the program because it will have been disposed.
You should look at System.Windows.Forms.Timer which is meant for using with GUI threads like this.
// INSTEAD, TRY THIS.
// It's really late here forgot to change some code after copy pasting from
above, should be good now.
static System.Windows.Forms.Timer timer1;
void button1_Click(object sender, EventArgs e)
{
timer1 = new System.Windows.Forms.Timer(60*60*1000);
timer1.Tick += Timer1_Tick;
timer1.Enabled = true;
WriteToSerialPort(); // Call method directly for writing to port.
}
private void timer1_Tick(object sender, EventArgs e)
{
WriteToSerialPort();
}
private void WriteToSerialPort()
{
serialPort1.Write("high"); // write to port
}
Here is another example that allows you to not have a dedicated method for writing to the serial port. I would want the dedicated method so the serial port could be written to outside of the timer tick event without having to write the serial port code more than once. The code below needs to be in try...catch blocks. Note: System.Windows.Forms.Timer here.
using System;
using System.Drawing;
using System.IO.Ports;
using System.Windows.Forms;
namespace SerialPortSample
{
public partial class Form1 : Form
{
private Timer timer1 = new Timer { Interval = 1000, Enabled = false }; // initialize timer, with a one second interval and disabled
private Button startTimerButton = new Button { Name = "startTimerButton",Text = #"Toggle Timer", Size = new Size(130, 33), Location = new Point(0, 0) };
// This is a place holder for the SerialPort control you probably have on your Form.
//Remove this instance of serialPort1 and use the serialPort1 control from your form.
private SerialPort serialPort1 = new SerialPort();
public Form1()
{
InitializeComponent();
// add button to Form
this.Controls.Add(startTimerButton); // add button to form1
startTimerButton.Click += StartTimerButton_Click;
timer1.Tick += Timer1_Tick; // attach timer tick event
}
private void StartTimerButton_Click(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled; // toggle timer.endabled, if false the Tick event will not be raised
timer1.Interval = 1000; // set timer interval to 1000 so the next time it is enabled it triggers immediately.
}
private void Timer1_Tick(object sender, EventArgs e)
{
timer1.Interval = 60 * 60 * 1000; // set timer interval to 1 hour so it will not trigger for an hour
if (!serialPort1.IsOpen)
serialPort1.Open(); // open serial port if not open
serialPort1.Write("high"); // write to the serial port
serialPort1.Close(); // close serial port
}
}
}
I would like to kill the first thread upon the second click, then re-create it. My goal is to only ever have one tick thread in existence.
I believe I can do this with supplying a delegate to the thread instance? but I have googled and fiddled with it for hours upon hours with no luck. Can anyone translate my anonymous lambda expression into something with a delegate so I can identify and kill my thread after I've started it?
I hope my question makes sense, I am still learning... thanks
Timer timer1 = new Timer();
timer1.Interval = 1000;
timer1.Start();
private void button1_Click(object sender, EventArgs e)
{
//If I click button twice, how do I kill the first tick thread,
//before creating another?
timer1.Tick += (sender2, e2) => timer1_Tick(sender2, e2, 1, "text");
}
private void timer1_Tick(object sender, EventArgs e, int num, string text)
{
//do stuff with num and text
}
All you have to do is create a variable to store the delegate -- that should be of type Action<object, EventArgs>. So something like this:
private Action<object, EventArgs> _handler;
private void button1_Click(object sender, EventArgs e)
{
if (_handler != null)
timer1.Tick -= _handler;
_handler = (sender2, e2) => timer1_Tick(sender2, e2, 1, "text");
timer1.Tick += _handler;
}
private void timer1_Tick(object sender, EventArgs e, int num, string text)
{
timer1.Tick -= _handler;
_handler = null;
//do stuff with num and text
}
Note that this creates a race condition -- the "_handler" reference could be set to null by a "timer1_Tick" thread after the other thread sets the reference, but before it adds it to the "Tick" event. (To avoid the race condition, you would want to create a local copy of "_handler".) Hopefully that gives you the idea though ...
Currently I'm moving from java to c# and I'm full of crazy questions.
I'm trying new things on a windows form application and now,I would like to create a loop wich is executing a code every 1 minute,the problem is that I have no idea where to put this code.
For example,the form structure is like:
using System;
namespace Tray_Icon
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
notifyIcon1.ShowBalloonTip(5000);
}
private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
{
label1.Text = "Baloon clicked!";
}
private void notifyIcon1_BalloonTipClosed(object sender, EventArgs e)
{
label1.Text = "baloon closed!";
}
private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
{
}
private void option1ToolStripMenuItem_Click(object sender, EventArgs e)
{
//some code here
}
private void option2ToolStripMenuItem_Click(object sender, EventArgs e)
{
//some code here
}
private void option3ToolStripMenuItem_Click(object sender, EventArgs e)
{
label1.Text = "Option 3 clicked!";
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
option1ToolStripMenuItem_Click(this, null);
}
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnWrite_Click(object sender, EventArgs e)
{
//code here
}
}
}
Where should I put the loop code? :(
Thanks in advance for ANY replay!!!
Add a Timer to your form:
set its Interval property to 60000 (one minute in milliseconds) and Enabled to True:
and attach an event handler to the Timer.Tick event, e.g. by double-clicking the timer in the Forms designer:
private void timer1_Tick(object sender, EventArgs e)
{
// do something here. It will be executed every 60 seconds
}
You would have to add a timer, and set the interval to 1000 miliseconds, and in the OnTick event you add the code with your loop
Timer tmr = null;
private void StartTimer()
{
tmr = new Timer();
tmr.Interval = 1000;
tmr.Tick += new EventHandler<EventArgs>(tmr_Tick);
tmr.Enabled = true;
}
void tmr_Tick(object sender, EventArgs e)
{
// Code with your loop here
}
You can't put any loop code in here.
In your designer look for the Timer control. When you have that, configure it to run every minute and place your code in the Timer_Tick event.
Or create a timer manually in code and respond to the event :) But for starters, doing it by the designer is easier!
Drag a Timer component on the Form and doubleclick it. There you go with the code.
The Timer component runs in the main thread so you can modify UI components without worrying.
Alternatively You could create a System.Timers.Timer, which has it's own thread and has some advantages, but possible caveats when modifying UI components. See http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
Try to use Background Worker and put the code in the backgroundWorker.DoWork or use a Timer
Use System.Timers.Timer:
System.Timers.Timer aTimer;
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 60000;
aTimer.Enabled = true;
}
private static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
}
for using Timer see this tutorial: C# Timer
How you do it in Java platform?
I think Java should be the same with .net.
In fact, a form program is just normal program which contains a event dispatcher. The event dispatcher listen to the UI events and dispatch them to the event handlers. I think all the UI mode should like this, no matter Java or .net platform.
So generally speaking, you have 2 options:
Start the loop at beginning. In this case, you should insert your
code in the constructor of the Form.
Start the loop when user
click the button. In this case, you should insert your code in the
event handler function.
Yes, as others mentioned, you should use the timer. But this should after you know where your code should locate. You also can use a endless loop with a sleep call. But timer is a better solution.
Idea of timer is more better. But If you want to use threads. Then Follow this
Let me assume that You want to do it right from the start of program
You can write in body of function (event in fact) named Form1_Load as
Your actual code is just within while loop other code only to guide
I can guide if you don't know the use of threads in C#
bool button2Clicked = false;
private void Form1_Load(object sender, EventArgs e)
{
// A good Way to call Thread
System.Threading.Thread t1 = new System.Threading.Thread(delegate()
{
while (!button2Clicked)
{
// Do Any Stuff;
System.Threading.Thread.Sleep(60000); //60000 Millieconds=1M
}
});
t1.IsBackground = true; // With above statement Thread Will automatically
// be Aborted on Application Exit
t1.Start();
}
I have problems with timer.
I have function in function (draw in func)
void func(){
/*...do something ... */
for(){
for() {
/*loop*/
draw(A,B, Pen);
}
/*... do something ...*/
}
}
This is draw function
public void draw1(Point Poc, Point Kra, Pen o) {
Graphics g = this.CreateGraphics();
g.DrawLine(o,Poc.X+4, Poc.Y+4,Kra.X+4, Kra.Y+4);
g.Dispose();
}
I call function 'func' on button click
private void button4_Click(object sender, EventArgs e){
func();
}
I want to call draw function evry second (draw the line every second).
Between drawings, function needs to continue working and calculate = loop, and draw next line for some time(interval). I tried with
timer1.Tick += new EventHandler(timer1_Tick);
etc..
private void timer1_Tick(object sender, EventArgs e)
{
...
draw(A, B, Pen)
}
etc..
but all that stops my function, and draw one random line.
I just want the time(interval) between two drawings in function 'func'. Without timer works fine, but draw all lines immediately, I need slow drawing.
Cheers.
I'm not entirely clear on what you're trying to do, but, in general, you can use an object of the Timer class to specify code to be executed on a specified interval. The code would look something like this:
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(DisplayTimeEvent);
myTimer.Interval = 1000; // 1000 ms is one second
myTimer.Start();
public static void DisplayTimeEvent(object source, ElapsedEventArgs e)
{
// code here will run every second
}
try this
var aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
//if your code is not registers timer globally then uncomment following code
//GC.KeepAlive(aTimer);
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
draw(A, B, Pen);
}
You don't draw in a WinForms app, you respond to update or paint messages. Do what you want to do in the form's Paint event (or override the OnPaint method). When you want the form to be re-drawn, use Form.Invalidate. e.g. call Form.Invalidate in the timer tick...
SOLVED for now with
System.Threading.Thread.Sleep(700);