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 issue

cross thread issue

Scheduled Pinned Locked Moved C#
helpcsharptutorialquestion
8 Posts 4 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.
  • U Offline
    U Offline
    User 10650102
    wrote on last edited by
    #1

    Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks

    public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
    try
    {
    RxString = serialPort1.ReadLine();
    label1.Text = RxString;
    }
    catch (Exception)
    {
    MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
    System.Threading.Thread.Sleep(5000);
    bool dataerror = true;
    DataRecError(dataerror);
    return;
    }
    this.Invoke(new EventHandler(DisplayText));
    i++;
    }

    Richard Andrew x64R B 2 Replies Last reply
    0
    • U User 10650102

      Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks

      public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
      {
      try
      {
      RxString = serialPort1.ReadLine();
      label1.Text = RxString;
      }
      catch (Exception)
      {
      MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
      System.Threading.Thread.Sleep(5000);
      bool dataerror = true;
      DataRecError(dataerror);
      return;
      }
      this.Invoke(new EventHandler(DisplayText));
      i++;
      }

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      See: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx[^]

      The difficult we do right away... ...the impossible takes slightly longer.

      U 1 Reply Last reply
      0
      • Richard Andrew x64R Richard Andrew x64

        See: http://msdn.microsoft.com/en-us/library/ms171728(v=vs.110).aspx[^]

        The difficult we do right away... ...the impossible takes slightly longer.

        U Offline
        U Offline
        User 10650102
        wrote on last edited by
        #3

        Thanks for the link, but I still cannot get it working.

        1 Reply Last reply
        0
        • U User 10650102

          Hello I'm new at c# and I'm running into a cross thread issue. I'm reading ASCII data from the serial port using the following code below: I will like to know how to pass RxString to a different Method without getting a cross thread error? Thanks

          public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
          {
          try
          {
          RxString = serialPort1.ReadLine();
          label1.Text = RxString;
          }
          catch (Exception)
          {
          MessageBox.Show("Data Receive Error: Close Port & Change Baud Rate" );
          System.Threading.Thread.Sleep(5000);
          bool dataerror = true;
          DataRecError(dataerror);
          return;
          }
          this.Invoke(new EventHandler(DisplayText));
          i++;
          }

          B Offline
          B Offline
          Bernhard Hiller
          wrote on last edited by
          #4

          The problem is the line

          label1.Text = RxString;

          It must not be executed in a thread different than the main thread. Try following code:

          if (InvokeRequired)
          {
          this.Invoke(new System.Action(() => { label1.Text = RxString; }));
          }
          else
          {
          // this is the main thread anyway
          label1.Text = RxString;
          }

          U B 2 Replies Last reply
          0
          • B Bernhard Hiller

            The problem is the line

            label1.Text = RxString;

            It must not be executed in a thread different than the main thread. Try following code:

            if (InvokeRequired)
            {
            this.Invoke(new System.Action(() => { label1.Text = RxString; }));
            }
            else
            {
            // this is the main thread anyway
            label1.Text = RxString;
            }

            U Offline
            U Offline
            User 10650102
            wrote on last edited by
            #5

            excellent, that worked. Thank you

            1 Reply Last reply
            0
            • B Bernhard Hiller

              The problem is the line

              label1.Text = RxString;

              It must not be executed in a thread different than the main thread. Try following code:

              if (InvokeRequired)
              {
              this.Invoke(new System.Action(() => { label1.Text = RxString; }));
              }
              else
              {
              // this is the main thread anyway
              label1.Text = RxString;
              }

              B Offline
              B Offline
              BobJanova
              wrote on last edited by
              #6

              I think you can just always call Invoke (or BeginInvoke) for this type of thing, those calls are harmless if called in the main thread.

              B 1 Reply Last reply
              0
              • B BobJanova

                I think you can just always call Invoke (or BeginInvoke) for this type of thing, those calls are harmless if called in the main thread.

                B Offline
                B Offline
                Bernhard Hiller
                wrote on last edited by
                #7

                I am not sure. In the example above, I used a lambda which does not call the function again, and that might work. But with "normal" code like

                delegate void VoidDelegate();
                void SomeFunction()
                {
                if (InvokeRequired)
                {
                this.Invoke(new VoidDelegate(SomeFunction));
                return;
                }
                // do something
                }

                you'll run into a StackOverflow exception when omitting the InvokeRequired... Hence I always use that "safer" version, though it might not be necessary in every case.

                B 1 Reply Last reply
                0
                • B Bernhard Hiller

                  I am not sure. In the example above, I used a lambda which does not call the function again, and that might work. But with "normal" code like

                  delegate void VoidDelegate();
                  void SomeFunction()
                  {
                  if (InvokeRequired)
                  {
                  this.Invoke(new VoidDelegate(SomeFunction));
                  return;
                  }
                  // do something
                  }

                  you'll run into a StackOverflow exception when omitting the InvokeRequired... Hence I always use that "safer" version, though it might not be necessary in every case.

                  B Offline
                  B Offline
                  BobJanova
                  wrote on last edited by
                  #8

                  That's a different situation though, that's a 'make this method self-marshalling' rather than 'marshal this method call'. The StackOverflowEx is pretty obvious if you don't guard this. If you put the responsibility on the caller (i.e. they must use control.Invoke(() => control.SomeFunction())), or you provide an explicit SomeFunctionAsync (using BeginInvoke) then it's not an issue. On a different note you don't need to define a void delegate type, MethodInvoker exists for this already.

                  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