Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Curious Cross Thread Error

Curious Cross Thread Error

Scheduled Pinned Locked Moved Visual Basic
helpquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheComputerMan
    wrote on last edited by
    #1

    Maybe it is not so curious, but it is to me at least. I have a system.timers.timer declared and set up and then the code executes the final line in the routine is ProcessDuplicateMessage. In the ProcessDuplicateMessage routine I get this error: System.InvalidOperationException was unhandled by user code Message="Cross-thread operation not valid: Control 'lblReviewValue' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.set_WindowText(String value) at System.Windows.Forms.Control.set_Text(String value) at System.Windows.Forms.Label.set_Text(String value) at ....FImportMsgProcess.ProcessDuplicateMessage() in C:\....\FImportMsgProcess.vb:line 41 at ....FImportMsgProcess.FormLoadComplete(Object sender, ElapsedEventArgs e) in C:\....\FImportMsgProcess.vb:line 134 at System.Timers.Timer.MyTimerCallback(Object state) Now obviously this is something to do with the timer, which I assume is running in a different thread, but my question is: How do I return the process to the thread for the form before calling the next routine. If I remove the system.timers.timer and add a forms.timer I don't get the problem. Just curious really since I have cured the immediate problem by using a forms.timer instead and I would like to know for another occasion. :)

    D 1 Reply Last reply
    0
    • T TheComputerMan

      Maybe it is not so curious, but it is to me at least. I have a system.timers.timer declared and set up and then the code executes the final line in the routine is ProcessDuplicateMessage. In the ProcessDuplicateMessage routine I get this error: System.InvalidOperationException was unhandled by user code Message="Cross-thread operation not valid: Control 'lblReviewValue' accessed from a thread other than the thread it was created on." Source="System.Windows.Forms" StackTrace: at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.set_WindowText(String value) at System.Windows.Forms.Control.set_Text(String value) at System.Windows.Forms.Label.set_Text(String value) at ....FImportMsgProcess.ProcessDuplicateMessage() in C:\....\FImportMsgProcess.vb:line 41 at ....FImportMsgProcess.FormLoadComplete(Object sender, ElapsedEventArgs e) in C:\....\FImportMsgProcess.vb:line 134 at System.Timers.Timer.MyTimerCallback(Object state) Now obviously this is something to do with the timer, which I assume is running in a different thread, but my question is: How do I return the process to the thread for the form before calling the next routine. If I remove the system.timers.timer and add a forms.timer I don't get the problem. Just curious really since I have cured the immediate problem by using a forms.timer instead and I would like to know for another occasion. :)

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      L T 2 Replies Last reply
      0
      • D Dave Kreskowiak

        You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008
        But no longer in 2009...

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        :rose:

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        D 1 Reply Last reply
        0
        • L Luc Pattyn

          :rose:

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          When I find a good article, I run with it.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
               2006, 2007, 2008
          But no longer in 2009...

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            When I find a good article, I run with it.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008
            But no longer in 2009...

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            I'll try and create some more then... :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read formatted code with indentation, so please use PRE tags for code snippets.


            I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


            1 Reply Last reply
            0
            • D Dave Kreskowiak

              You're correct about the System.Timers.Timer "firing" your code on a seperate thread. Docs on the Timer class are here[^] You also cannot access UI controls from any other thread other than the thread that created the control, i.e.: the UI (or startup) thread. A good article on the hows and whys of this can be found here[^].

              A guide to posting questions on CodeProject[^]
              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                   2006, 2007, 2008
              But no longer in 2009...

              T Offline
              T Offline
              TheComputerMan
              wrote on last edited by
              #6

              Thanks Dave, and of course Luc for the article.

              L 1 Reply Last reply
              0
              • T TheComputerMan

                Thanks Dave, and of course Luc for the article.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                You're welcome. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read formatted code with indentation, so please use PRE tags for code snippets.


                I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups