Hi everyone,
I have a Socket derived class that parses the received data into a message,
populates a object with that information and then tries to send an event to
inform the owner of the socket that there is message to be processed. But
this is not working as the socket receiver method seems to be in a different
thread then the form that owns the socket.
The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.
The method that handles this event needs to change some controls on the form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.
I would appreciate any suggestions on solving this.
class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;
public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new ProcessMessageA rgs(Msg));
}
}
}
class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);
I would have had to use PostThreadMessa ge in native windows development, but
I can't figure out what I need to use in this situation.
AliR.
I have a Socket derived class that parses the received data into a message,
populates a object with that information and then tries to send an event to
inform the owner of the socket that there is message to be processed. But
this is not working as the socket receiver method seems to be in a different
thread then the form that owns the socket.
The receive callback gets the data from the socket and process the
information, once it constructs a full message from it then it calls a
delegate if the delegate is set.
The method that handles this event needs to change some controls on the form
and when I do that I get the exception:
Cross-thread operation not valid: Control 'syncButton' accessed from a
thread other than the thread it was created on.
I would appreciate any suggestions on solving this.
class LSSocket : Socket
{
public event ProcessMessageH andler ProcessMessageE vent;
public void OnReceiveData(I AsyncResult)
{
....
if (ProcessMessage Event != null)
{
ProcessMessageE vent(this, new ProcessMessageA rgs(Msg));
}
}
}
class ProcessMessageA rgs : System.EventArg s
{
public ProcessMessageA rgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageH andler(object sender, ProcessMessageA rgs
e);
I would have had to use PostThreadMessa ge in native windows development, but
I can't figure out what I need to use in this situation.
AliR.
Comment