473,385 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Show Elapsed time of Windows Form - Experincing Cross Thread Error

Hello,

In my class I'm trying to display the elapsed time as my label on a windows form. I've been reading up on all the different articles online and have tried these three methods all with the same result:

1. Using the System.Timers class
2. Built in Timer sub-class
3. Defining directly inside of my class the StartTime, etc. variables.

All of these still result in the following error:
Cross-thread operation not valid: Control 'User_Interface' accessed from a thread other than the thread it was created on.

Currently, my windows form is being called by another class, but I'm not sure if this is the cause of the problem.

This is a sample of the code that I'm currently using to show the elapsed time:
Expand|Select|Wrap|Line Numbers
  1. private void OnTimedEvent (object source, ElapsedEventArgs c)
  2.         {
  3.             TimeSpan time;
  4.  
  5.             currentTime = DateTime.Now;
  6.             time = currentTime - startTime;
  7.  
  8.  
  9.             int days = time.Days;
  10.             double hours = time.Hours;
  11.             double mins = time.Minutes;
  12.             double secs = time.Seconds;
  13.             if (days != 0)
  14.             {
  15.                 elapsedTime += days.ToString() + ":";
  16.             }
  17.             if (hours != 0)
  18.             {
  19.                 elapsedTime += hours.ToString("00") + ":";
  20.             }
  21.             elapsedTime += mins.ToString("00") + ":";
  22.             elapsedTime += secs.ToString("00");
  23.  
  24.             UI_Time_label.Text = elapsedTime;
  25.  
Help?
Aug 6 '12 #1

✓ answered by Yammyrammy

For anyone else experiencing something similar to this, I found the solution online. The reason a cross thread error (or multi-thread) is occurring is because of the Timer. The timer is attempting to perform it's method on the running thread, when it really needs to be on another, correct thread. It's a relatively simple fix in the code.

I found my solution here: http://stackoverflow.com/questions/2...ms-application

5 3843
kadghar
1,295 Expert 1GB
1) Place a timer in your form.
2) Set its interval to 1 second (1000 milsecs)
3) In the tick method, add 1 to your label's value.

--> Make sure the timer is enabled.
Aug 7 '12 #2
Thank you for the reply. By label value I'm assuming you mean the label's text? If so, I've attempted your suggested solution before (with my timer enabled), but with the same exception thrown. I'm also not trying to record the timer onto the label; I'm trying to record the elapsed time.
Aug 7 '12 #3
kadghar
1,295 Expert 1GB
Yes, i ment the label's text converted into a numeric value.

But now that i see your problem. perhaps you should make sure the form is public and also all its objects and methods.
Aug 8 '12 #4
For anyone else experiencing something similar to this, I found the solution online. The reason a cross thread error (or multi-thread) is occurring is because of the Timer. The timer is attempting to perform it's method on the running thread, when it really needs to be on another, correct thread. It's a relatively simple fix in the code.

I found my solution here: http://stackoverflow.com/questions/2...ms-application
Aug 11 '12 #5
Frinavale
9,735 Expert Mod 8TB
Just another note, for those of you who are experiencing this error in a WPF or Silverlight application, you need to use the Dispatcher class to update your label appropriately.

-Frinny
Aug 13 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Brian Keating EI9FXB | last post by:
Hello all, Wonder what approach is used for this problem. I have a MDIApplication, the MDIClinets are to be in a seperate thread. So I've done something like this, // Create a new Show...
4
by: BobAchgill | last post by:
What is the best way to show a video file directly in a Form? Can I use functions of Windows Player in a Windows Form? Functions like: play video or show visualization for music, etc.
10
by: Daniel | last post by:
Hi guys I have a form with my gui on, just some list boxes. And a class that handles incoming data. i want to, on receiving data, update my gui. However even though i have an instance...
3
by: Jack | last post by:
I have an application (COM Add-in) that in the initialization code it displays a splash screen. I want the splash screen to close after n seconds , so I add a timer to the splash screen and...
3
by: Alan T | last post by:
I want to put an icon on a Windows Form top left hand corner but would like to hide the maximize and close box on the top right hand corner. Can I do that?
5
by: nospam | last post by:
Hi all. I have encountered a "Cross-thread operation not valid" error in a test program I created and although I have came up with a solution I don't like the performance of the program. I...
3
by: jehugaleahsa | last post by:
Hello: I have a business object that reports the updated status of a download taking place in another thread. I wanted to simplify my code by having the interface bind to my business object...
3
by: kimiraikkonen | last post by:
Hi, I was looking for an example on CodeProject and saw an interesting thing, i downloaded the article source code and converted to my VB 2005 Express and compiled with no problem, however when i...
7
by: hatch | last post by:
The project I am working on has to have start and stop buttons on a form and a label to show the eslaped time once the start button is pushed. The elapsed tome should stop when the stop button is...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.