Show Elapsed time of Windows Form - Experincing Cross Thread Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Yammyrammy
    New Member
    • Aug 2012
    • 3

    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:
    Code:
    private void OnTimedEvent (object source, ElapsedEventArgs c)
            {
                TimeSpan time;
    
                currentTime = DateTime.Now;
                time = currentTime - startTime;
    
    
                int days = time.Days;
                double hours = time.Hours;
                double mins = time.Minutes;
                double secs = time.Seconds;
                if (days != 0)
                {
                    elapsedTime += days.ToString() + ":";
                }
                if (hours != 0)
                {
                    elapsedTime += hours.ToString("00") + ":";
                }
                elapsedTime += mins.ToString("00") + ":";
                elapsedTime += secs.ToString("00");
    
                UI_Time_label.Text = elapsedTime;
    Help?
    Last edited by PsychoCoder; Aug 12 '12, 05:25 AM.
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    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.

    Comment

    • Yammyrammy
      New Member
      • Aug 2012
      • 3

      #3
      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.

      Comment

      • kadghar
        Recognized Expert Top Contributor
        • Apr 2007
        • 1302

        #4
        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.

        Comment

        • Yammyrammy
          New Member
          • Aug 2012
          • 3

          #5
          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

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            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

            Comment

            Working...