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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. How come another thread can update main controls without invoking them? [modified]

How come another thread can update main controls without invoking them? [modified]

Scheduled Pinned Locked Moved C#
helpquestionannouncement
8 Posts 5 Posters 1 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.
  • Y Offline
    Y Offline
    yeah1000
    wrote on last edited by
    #1

    Hi, i have a strange problem. I can update any main thread control from a different thread without any cross thread exceptions. I can change the text of a textbox from the thread and from any button click handler in main thread. What are the possible causes of this kind of behavior? Can it somehow remember that i have used invoke on the controls? When i do the exact same thing in a new project i get the crossthread exception. An illustration of what i mean: private void button1_Click(object sender, EventArgs e) { Thread someThread = new Thread(Threadfun); someThread.Start(); } private void Threadfun() { label1.Text = "hello"; } ty

    modified on Thursday, February 25, 2010 10:50 AM

    S L R 3 Replies Last reply
    0
    • Y yeah1000

      Hi, i have a strange problem. I can update any main thread control from a different thread without any cross thread exceptions. I can change the text of a textbox from the thread and from any button click handler in main thread. What are the possible causes of this kind of behavior? Can it somehow remember that i have used invoke on the controls? When i do the exact same thing in a new project i get the crossthread exception. An illustration of what i mean: private void button1_Click(object sender, EventArgs e) { Thread someThread = new Thread(Threadfun); someThread.Start(); } private void Threadfun() { label1.Text = "hello"; } ty

      modified on Thursday, February 25, 2010 10:50 AM

      S Offline
      S Offline
      Saksida Bojan
      wrote on last edited by
      #2

      Are you compiled it under .NET framework? I know that in .NET2.0 was added detection.

      1 Reply Last reply
      0
      • Y yeah1000

        Hi, i have a strange problem. I can update any main thread control from a different thread without any cross thread exceptions. I can change the text of a textbox from the thread and from any button click handler in main thread. What are the possible causes of this kind of behavior? Can it somehow remember that i have used invoke on the controls? When i do the exact same thing in a new project i get the crossthread exception. An illustration of what i mean: private void button1_Click(object sender, EventArgs e) { Thread someThread = new Thread(Threadfun); someThread.Start(); } private void Threadfun() { label1.Text = "hello"; } ty

        modified on Thursday, February 25, 2010 10:50 AM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        private void Form1_Load(object sender, EventArgs e)
        {
        Thread t = new Thread(RunMyThread);
        t.IsBackground = true;
        t.Start();
        }

        private void RunMyThread()
        {
        Invoke(new MethodInvoker(InvokeLabel));
        }

        private void InvokeLabel()
        {
        label1.Text = "Hello World";
        }

        Y 1 Reply Last reply
        0
        • L Lost User

          private void Form1_Load(object sender, EventArgs e)
          {
          Thread t = new Thread(RunMyThread);
          t.IsBackground = true;
          t.Start();
          }

          private void RunMyThread()
          {
          Invoke(new MethodInvoker(InvokeLabel));
          }

          private void InvokeLabel()
          {
          label1.Text = "Hello World";
          }

          Y Offline
          Y Offline
          yeah1000
          wrote on last edited by
          #4

          .Net framework 3.5 stancrm, i know that this is the way to go but my problem was that i was able to chagne the parameter without invoking :)

          S L 2 Replies Last reply
          0
          • Y yeah1000

            .Net framework 3.5 stancrm, i know that this is the way to go but my problem was that i was able to chagne the parameter without invoking :)

            S Offline
            S Offline
            Saksida Bojan
            wrote on last edited by
            #5

            The only whay that it doesn't throw you is that you either used try catch to handle error or that control is created on a same thread as thread that is modifying your control. You can check AnyControl.InvokeRequired proprty

            1 Reply Last reply
            0
            • Y yeah1000

              .Net framework 3.5 stancrm, i know that this is the way to go but my problem was that i was able to chagne the parameter without invoking :)

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

              some Control operations are harmless and work across threads without causing an exception; example: reading Label.Text. As this is undocumented, you should not rely on it. I collected the most relevant information here[^]. :)

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


              I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
              All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


              1 Reply Last reply
              0
              • Y yeah1000

                Hi, i have a strange problem. I can update any main thread control from a different thread without any cross thread exceptions. I can change the text of a textbox from the thread and from any button click handler in main thread. What are the possible causes of this kind of behavior? Can it somehow remember that i have used invoke on the controls? When i do the exact same thing in a new project i get the crossthread exception. An illustration of what i mean: private void button1_Click(object sender, EventArgs e) { Thread someThread = new Thread(Threadfun); someThread.Start(); } private void Threadfun() { label1.Text = "hello"; } ty

                modified on Thursday, February 25, 2010 10:50 AM

                R Offline
                R Offline
                Rob Philpott
                wrote on last edited by
                #7

                There is a setting you could check: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx Interesting, according to that page exceptions are only thrown if debugging (which I hadn't realised). Are you debugging?

                Regards, Rob Philpott.

                L 1 Reply Last reply
                0
                • R Rob Philpott

                  There is a setting you could check: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.checkforillegalcrossthreadcalls.aspx Interesting, according to that page exceptions are only thrown if debugging (which I hadn't realised). Are you debugging?

                  Regards, Rob Philpott.

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

                  Rob Philpott wrote:

                  exceptions are only thrown if debugging

                  Didn't know that and so I checked it with a Threading.Timer modifying a Label.Text; throws an exception inside Visual Studio; however even a debug build will not throw an exception if run outside Visual Studio. :)

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


                  I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
                  All Toronto weekends should be extremely wet until we get it automated in regular forums, not just QA.


                  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