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. Passing data from a child thread

Passing data from a child thread

Scheduled Pinned Locked Moved C#
csharpc++helptutorialquestion
3 Posts 3 Posters 2 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.
  • V Offline
    V Offline
    vikingsimon
    wrote on last edited by
    #1

    I don't know how to pass data from a child thread to the parent thread. I want to write a child thread that is reading data from a serial port and pass the data to the parent thread that updates a TextBox. I think I have to use callback with delegates, but I don't know how to do that. Does anybody have a code sample in C#.NET or C++.NET? If would be very grateful if somebody could help me.

    C 1 2 Replies Last reply
    0
    • V vikingsimon

      I don't know how to pass data from a child thread to the parent thread. I want to write a child thread that is reading data from a serial port and pass the data to the parent thread that updates a TextBox. I think I have to use callback with delegates, but I don't know how to do that. Does anybody have a code sample in C#.NET or C++.NET? If would be very grateful if somebody could help me.

      C Offline
      C Offline
      Cracked Down
      wrote on last edited by
      #2

      number of sites are present dictating about your problem you guys are realy lazy!!!! even you wont get time to do some googling!!!!!!!!!!! please first search on the net if you dont find it then post the question.......................

      1 Reply Last reply
      0
      • V vikingsimon

        I don't know how to pass data from a child thread to the parent thread. I want to write a child thread that is reading data from a serial port and pass the data to the parent thread that updates a TextBox. I think I have to use callback with delegates, but I don't know how to do that. Does anybody have a code sample in C#.NET or C++.NET? If would be very grateful if somebody could help me.

        1 Offline
        1 Offline
        12Code
        wrote on last edited by
        #3

        In your serial port receive event, Test class, add an event:

            private void Test\_DataReceived(object sender, EventArgs e)
            {
                    // do RxText here...
        
                    OnEventRxCompleted(RxText); 
            }
        
            public delegate void TestEventHandler(string Text);
            public event TestEventHandler EventRxCompleted;
            protected void OnEventRxCompleted(string RxText)
            {
                if (EventRxCompleted != null)
                {                           
                    EventRxCompleted(RxText); // Fire event now              
                }
            }
        

        in Parent from: add this in constructor,

        Test.EventRxCompleted+= new Test.TestEventHandler(EventDataReceived);

        and also,

            void EventDataReceived(string Text)
            {
                SetText(Text);
            }
        
            delegate void SetTextCallback(string text);
            private void SetText(string text)
            {
                if (txtParent.InvokeRequired) // txtParent is TextBox in parent form
                {
                    SetTextCallback d = new SetTextCallback(SetText);
                    this.Invoke(d, new object\[\] { text });
                }
                else
                {
                    txtParent.Text = text;
                }
            }
        

        Kelvin

        modified on Friday, March 20, 2009 5:14 AM

        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