473,486 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

using ElapsedEventHandler and Label

Its supposed to be a very simple program, with a class to display the
updated current time every second (CAnalogTime) inside a Label in my
form, using ElapsedEventHandler from System.Timers;

The event is working, because the displaybox will show up every second
like requested.
Although the label (which i was able to initialize with a value), wont
update itself in this event.

Any ideas?

Thanx
--------------------------

public class CAnalogTime
{
public System.Timers.Timer myTimer;
public MainForm myForm;

public CAnalogTime( MainForm inForm)
{

myForm = inForm;
// There, the label will update normally ---->
myForm.label1.Text = "dfdstime";
myTimer = new System.Timers.Timer();
myTimer.Interval = 1000;
myTimer.Enabled = true;
myTimer.Elapsed += new ElapsedEventHandler(myForm.CheckInterval);
myTimer.Start();
}
}

public partial class MainForm
{
[STAThread]
public static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
Application.Run(new MainForm());
}
public MainForm()
{
InitializeComponent();
CAnalogTime myAnalogClock = new CAnalogTime( this);
}
public void CheckInterval(object sender,
System.Timers.ElapsedEventArgs e)
{
// But here the label wont update
this.label1.Text = DateTime.Now.ToLongTimeString();
// And strangely, the textbox will display every second :
MessageBox.Show("toto");
}
}
--------------------------

Mar 27 '06 #1
3 5409
The following should work BUT PLEASE READ the following paragraph:
after your this.label1.Text = DateTime.Now.ToLongTimeString(); add the
following
this.label1.Refresh();
or
Application.DoEvents();

Application DoEvents() will allow your app to yield to allow Windows to
process any pending events such as redrawing invalid parts of the
screen.

*** MULTI-THREADING ISSUES ***
One of the most important things to bear in mind with multithreading is
that you MUST ONLY communicate with control using the thread that
created, ie your main app. By using a thread from the ThreadPool,
created by using the Timer class from System.Timers class you have
created a new thread and have borken that rule.

It might be OK with a little app but can create subtle reliability
issues that are really hard to track down and replicate.

There's loads of good info around about this subject. I'd also
recommend .NET Multhreading by Alan L Dennis (ISBN 1-930110-54-5)

The quickest answer is to loook at the InvokeRequired property on
this.label1 to see if you're in a different thread and use
this.label1.Invoke if you are. Here's the code you could try:

public void CheckInterval(object sender, System.Timers.ElapsedEventArgs
e)
{
InvokeUpdateLabel();
}
private void InvokeUpdateLabel()
{
if (this.label1.InvokeRequired)
{
this.label1.Invoke(new MethodInvoker(InvokeUpdateLabel));
}
else
{
this.label1.Text = DateTime.Now.ToLongTimeString();
this.label1.Refresh();
}
}

You can see that it just reinvokes itself in the correct thread

Jason

Mar 27 '06 #2
>Any ideas?

I suggest you use the System.Windows.Forms.Timer component instead.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Mar 27 '06 #3
>I'd suggest you use the System.Windows.Forms.Timer component instead.
Good point, it will run be in the same thread so need to worry about
Invoke/InvokeRequired.

But if CheckInterval() starts to take more than a few seconds to run
(as these things have a tendency to do the more they get developed)
you'll end up blocking the main thread resulting in a slow UI and hence
slow redrawing of thr screen

Mar 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
8489
by: clintonG | last post by:
Briefly stated, my problem is accessing and 'setting' properties of a label control declared in the template of another control noting that the other control is an instance of the beta 2...
2
3020
by: DaveF | last post by:
public void StartTimer() { System.Timers.Timer myTimer = new System.Timers.Timer(); myTimer.Interval = Convert.ToDouble(ConfigurationSettings.AppSettings); myTimer.Elapsed += new...
1
13367
by: Max Adams | last post by:
Using System.Timers.ElapsedEventHandler to specify a method and and ElapsedEventArgs object I've trawled the internet looking for some help on this topic. What I want to do is, every x seconds...
5
2129
by: ElanKathir | last post by:
Hi ! I wrote one code for Send the E-mail, But that code have some problem , So please help me Here i paste my code and Error: Error: Server Error in '/Elan_Sample' Application. ...
3
5310
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look...
8
2388
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
2174
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
0
2534
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
6
2259
by: IReallyNeedHelp | last post by:
I have saved the questions using AddQuestion.aspx page i have created but i don't know how to display it and calculate their score. this is the formview i have done, but there is some error ...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7126
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7175
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5434
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.