Hi,
I've a component that use Asynchronic Sockets to comunicate with the server
component. In a Desktop Application, wich use the component, in the main
form open a form in dialog mode and add events handlers to the instance of
the frmMain.MyClientComponent. When i receive events from the component
those are handled and i do this.Invoke to work in the UI Thread. This work
ok until i close the dialog form, when i open the dialog box again when
receive an event and do this.Invoke the next error are raised:
"An unhandled exception of type 'System.InvalidOperationException' occurred
in MyClientComponent.dll
Additional information: Cannot call Invoke or InvokeAsync on a control until
the window handle has been created."
Sorry for my bad english, I hope you can understand me.
And code sample to understand better:
....
MyClientComponent.dll <-- Use Asynchronic Sockets and has ImcomingMessage
event.
....
....//frmMain
MyClientComponent m_ClientComponent = new MyClientComponent();
private void OpenReceiveMessagesForm()
{
frmReceiveMessages ReceiveMessagesForm = new
frmReceiveMessages(m_ClientComponent);
ReceiveMessagesForm.ShowDialog(this);
ReceiveMessagesForm.Dispose();
ReceiveMessagesForm = null;
}
....
....//frmReceiveMessages
MyClientComponent m_ClientComponent
delegate void MessageReceivedEventHandler(IncomingMessageEventAr gs e);
public frmReceiveMessages(MyClientComponent clientComponent)
{
m_ClientComponent = clientComponent;
m_ClientComponent.ImcomingMessage += new
ImcomingMessageEventHandler(m_ClientComponent_Imco mingMessage);
}
private void m_ClientComponent_ImcomingMessage(object sender,
IncomingMessageEventArgs e)
{
try
{
//The first time that is opened this form this method works fine, if i
close this form and re open again, just here fire the error descripted
this.Invoke(new
MessageReceivedEventHandler(m_ClientComponent_Imco mingMessage), new
object[]{e});
}
catch(Exception ex)
{
//An unhandled exception of type 'System.InvalidOperationException'
occurred in MyClientComponent.dll Additional information: Cannot call Invoke
or InvokeAsync on a control until the window handle has been created.
throw ex;
}
}
private void m_ClientComponent_ImcomingMessage(IncomingMessageE ventArgs e)
{
lblMessagesReceived.Text += e.Message + "\r\n";
}
....