A
AliR \(VC++ MVP\)
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 ProcessMessageHandler ProcessMessageEvent;
public void OnReceiveData(IAsyncResult)
{
....
if (ProcessMessageEvent != null)
{
ProcessMessageEvent(this, new ProcessMessageArgs(Msg));
}
}
}
class ProcessMessageArgs : System.EventArgs
{
public ProcessMessageArgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageHandler(object sender, ProcessMessageArgs
e);
I would have had to use PostThreadMessage 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 ProcessMessageHandler ProcessMessageEvent;
public void OnReceiveData(IAsyncResult)
{
....
if (ProcessMessageEvent != null)
{
ProcessMessageEvent(this, new ProcessMessageArgs(Msg));
}
}
}
class ProcessMessageArgs : System.EventArgs
{
public ProcessMessageArgs(LSMessage msg)
{
this.msg = msg;
}
public LSMessage msg;
}
delegate void ProcessMessageHandler(object sender, ProcessMessageArgs
e);
I would have had to use PostThreadMessage in native windows development, but
I can't figure out what I need to use in this situation.
AliR.