Hi Mo,
sorry, the sample code I sent previously was not complete - note to self
"don't post when very late :-)" when you call Invoke if you pass parameters
to your delegate then you also need to specify those in the call to invoke,
as values of an object array, so:
this.TXNumber.Invoke(new
SetTextValueHander(SetTextValue(tempstring)));
would be:
this.TXNumber.Invoke(new SetTextValueHandler(SetTextValue(tempString)), new
object[]{tempString});
The idea behind calling invoke is that you can only modify a control on the
same thread that created the control. The sp_DataReceived function is called
in the context of a different thread, one that is used to receive the data
from your serial port, so calling invoke will swap control back to the thread
that created the textbox (Since you are calling Invoke from the textbox
object). The delegate is like a strongly typed function pointer, the
delegate defines the method signature that it can point to, in our case a
method which has a return of void and takes a string parameter, so saying:
new SetTextValueHandler(SetTextValue)
is creating a function pointer in effect to the SetTextValue that the
textbox control should call once it gets control.
In your case you are processing data so either you need to make sure the
call to the method you call is fast or you can use the BeginInvoke which is
asyncronous and does not wait for the method pointed to by the delegate to
complete.
Hope that helps.
Thanks
Mark.
--
http://www.markdawson.org
"Mo" wrote:
Thank you for the response. This process is still a mystery to me. I am
getting Mthod name expected in the line
this.TXNumber.Invoke(new SetTextValueHander(SetTextValue(tempstring)));
Any Ideas?
The code to look like:
delegate void SetTextValueHandler(string value);
void SetTextValue(string value)
{
this.TXNumber.Text = value;
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string ret = "\r";
string tempstring = sp.ReadLine().Replace(ret, "");
sp.Close();
this.TXNumber.Invoke(new
SetTextValueHander(SetTextValue(tempstring)));
if (tempstring.Length0)
{
Run_process();
sp.Open();
}
}