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