Playing round with Timers.
Context: a winforms with two labels.
I would like to see how System.Timers.Timer works so I’ve not used the Forms timer.
I understand that the form and myTimer will now be running in different threads.
Is there an easy way to represent the elapsed time on lblValue in the following form?
I’ve looked here on MSDN but is there an easier way !
Here’s the winforms code:
using System.Timers;
namespace Ariport_Parking
{
public partial class AirportParking : Form
{
//instance variables of the form
System.Timers.Timer myTimer;
int ElapsedCounter = 0;
int MaxTime = 5000;
int elapsedTime = 0;
static int tickLength = 100;
public AirportParking()
{
InitializeComponent();
keepingTime();
lblValue.Text = "hello";
}
//method for keeping time
public void keepingTime() {
myTimer = new System.Timers.Timer(tickLength);
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.AutoReset = true;
myTimer.Enabled = true;
myTimer.Start();
}
void myTimer_Elapsed(Object myObject,EventArgs myEventArgs){
myTimer.Stop();
ElapsedCounter += 1;
elapsedTime += tickLength;
if (elapsedTime < MaxTime)
{
this.lblElapsedTime.Text = elapsedTime.ToString();
if (ElapsedCounter % 2 == 0)
this.lblValue.Text = "hello world";
else
this.lblValue.Text = "hello";
myTimer.Start();
}
else
{ myTimer.Start(); }
}
}
}
I guess your code is just a test so I won’t discuss about what you do with your timer. The problem here is how to do something with an user interface control inside your timer callback.
Most of
Control‘s methods and properties can be accessed only from the UI thread (in reality they can be accessed only from the thread where you created them but this is another story). This is because each thread has to have its own message loop (GetMessage()filters out messages by thread) then to do something with aControlyou have to dispatch a message from your thread to the main thread. In .NET it is easy because everyControlinherits a couple of methods for this purpose:Invoke/BeginInvoke/EndInvoke. To know if executing thread must call those methods you have the propertyInvokeRequired. Just change your code with this to make it works:Please check MSDN for the list of methods you can call from any thread, just as reference you can always call
Invalidate,BeginInvoke,EndInvoke,Invokemethods and to readInvokeRequiredproperty. In general this is a common usage pattern (assumingthisis an object derived fromControl):Note that current thread will block until UI thread completed method execution. This may be an issue if thread’s timing is important (do not forget that UI thread may be busy or hung for a little). If you don’t need method’s return value you may simply replace
InvokewithBeginInvoke, for WinForms you don’t even need subsequent call toEndInvoke:If you need return value then you have to deal with usual
IAsyncResultinterface.How it works?
A GUI Windows application is based on the window procedure with its message loops. If you write an application in plain C you have something like this:
With these few lines of code your application wait for a message and then delivers the message to the window procedure. The window procedure is a big switch/case statement where you check the messages (
WM_) you know and you process them somehow (you paint the window forWM_PAINT, you quit your application forWM_QUITand so on).Now imagine you have a working thread, how can you call your main thread? Simplest way is using this underlying structure to do the trick. I oversimplify the task but these are the steps:
Both WPF and WinForms use this method to deliver (dispatch) a message from a thread to the UI thread. Take a look to this article on MSDN for more details about multiple threads and user interface, WinForms hides a lot of these details and you do not have to take care of them but you may take a look to understand how it works under the hood.