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. C#
  4. cross thread

cross thread

Scheduled Pinned Locked Moved C#
question
12 Posts 5 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.
  • Y Offline
    Y Offline
    Yustme
    wrote on last edited by
    #1

    Hi, How can I make a safe cross thread call for a textbox to set the 'text' of the textbox?

    D L 2 Replies Last reply
    0
    • Y Yustme

      Hi, How can I make a safe cross thread call for a textbox to set the 'text' of the textbox?

      D Offline
      D Offline
      Dan Mos
      wrote on last edited by
      #2

      Use the InvokeRequired and if required Invoke() it. Else normal assignement.

      Y 1 Reply Last reply
      0
      • D Dan Mos

        Use the InvokeRequired and if required Invoke() it. Else normal assignement.

        Y Offline
        Y Offline
        Yustme
        wrote on last edited by
        #3

        Hi, Do you got a sample for me? Thanks in advance!

        D D 2 Replies Last reply
        0
        • Y Yustme

          Hi, Do you got a sample for me? Thanks in advance!

          D Offline
          D Offline
          Dan Mos
          wrote on last edited by
          #4

          something like:

          if(this.txt1.InvokeRequiered){

          Invoke(new MethodInvoker(delegate
          {
          this.txt1.Text = ComputedValue();
          }));

          Something like that. [Edit]This example assumes that you created your working thread from the UI thread[/Edit]

          modified on Sunday, February 28, 2010 12:50 PM

          A 1 Reply Last reply
          0
          • D Dan Mos

            something like:

            if(this.txt1.InvokeRequiered){

            Invoke(new MethodInvoker(delegate
            {
            this.txt1.Text = ComputedValue();
            }));

            Something like that. [Edit]This example assumes that you created your working thread from the UI thread[/Edit]

            modified on Sunday, February 28, 2010 12:50 PM

            A Offline
            A Offline
            Alex Manolescu
            wrote on last edited by
            #5

            You can either disable cross thread exception or you can use delegates. My advice, use delegates! :) Cheer's, Alex Manolescu.

            D 1 Reply Last reply
            0
            • Y Yustme

              Hi, Do you got a sample for me? Thanks in advance!

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              I do pretty much the same as Moshu except I invoke the method again from within the method if required (recursion) and repass the parameters rather than hardcoding them.

              private void SetText(Control control, string text)
              {
              if (InvokeRequired)
              Invoke(new MethodInvoker(delegate { SetText(textBox, text); }));
              else
              control.Text = text;
              }

              Dave
              Tip: Passing values between objects using events (C#)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
              Why are you using VB6? Do you hate yourself? (Christian Graus)

              Y 1 Reply Last reply
              0
              • D DaveyM69

                I do pretty much the same as Moshu except I invoke the method again from within the method if required (recursion) and repass the parameters rather than hardcoding them.

                private void SetText(Control control, string text)
                {
                if (InvokeRequired)
                Invoke(new MethodInvoker(delegate { SetText(textBox, text); }));
                else
                control.Text = text;
                }

                Dave
                Tip: Passing values between objects using events (C#)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                Why are you using VB6? Do you hate yourself? (Christian Graus)

                Y Offline
                Y Offline
                Yustme
                wrote on last edited by
                #7

                Hi, How do you make the control enter that method? Shouldn't there be some sort of callback declared?

                D 1 Reply Last reply
                0
                • A Alex Manolescu

                  You can either disable cross thread exception or you can use delegates. My advice, use delegates! :) Cheer's, Alex Manolescu.

                  D Offline
                  D Offline
                  Dan Mos
                  wrote on last edited by
                  #8

                  Nice to here of you again. Now romanian stuff. Ce mai zici? N-ai m-ai raspuns. In ce parti ale tarii esti? Oras. In ce domeniu(specific) lucri? Back to english => I'm working on a System.Terror implementaion. Would anyone like to help me in my effort? It curently implements the ISpanihInquistion, LiquidNitrogen, AbsDoDoubtlyNitro and many many others such as IDoublyDoubtAnything, ISuck, IPlz , IWantFerrari and so on. [Edit] And of course **IForgatToMention** that **IAmInsane**. [/edit] cheers

                  modified on Sunday, February 28, 2010 1:36 PM

                  A 1 Reply Last reply
                  0
                  • Y Yustme

                    Hi, How do you make the control enter that method? Shouldn't there be some sort of callback declared?

                    D Offline
                    D Offline
                    DaveyM69
                    wrote on last edited by
                    #9

                    However you like, you can call that method from a callback if desired. If you are wanting to handle an event from a different thread without calling another method then just match up to the event signature as you do in your event handler to invoke the handler on the correct thread.

                    Dave
                    Tip: Passing values between objects using events (C#)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Why are you using VB6? Do you hate yourself? (Christian Graus)

                    1 Reply Last reply
                    0
                    • Y Yustme

                      Hi, How can I make a safe cross thread call for a textbox to set the 'text' of the textbox?

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

                      This[^] deals with it. :)

                      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.


                      Y 1 Reply Last reply
                      0
                      • L Luc Pattyn

                        This[^] deals with it. :)

                        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.


                        Y Offline
                        Y Offline
                        Yustme
                        wrote on last edited by
                        #11

                        Thank you guys, it works! :)

                        1 Reply Last reply
                        0
                        • D Dan Mos

                          Nice to here of you again. Now romanian stuff. Ce mai zici? N-ai m-ai raspuns. In ce parti ale tarii esti? Oras. In ce domeniu(specific) lucri? Back to english => I'm working on a System.Terror implementaion. Would anyone like to help me in my effort? It curently implements the ISpanihInquistion, LiquidNitrogen, AbsDoDoubtlyNitro and many many others such as IDoublyDoubtAnything, ISuck, IPlz , IWantFerrari and so on. [Edit] And of course **IForgatToMention** that **IAmInsane**. [/edit] cheers

                          modified on Sunday, February 28, 2010 1:36 PM

                          A Offline
                          A Offline
                          Alex Manolescu
                          wrote on last edited by
                          #12

                          Hi Mosule, i've been busy with my work :) i'll send you a pm. cheers

                          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