async callback problem

  • Thread starter Thread starter jesbin
  • Start date Start date
J

jesbin

Hi,
I'm using a async callback mechanism to receive data from
the socket. Once the data is received, I parse the data
and if certain information is received I open a new form.
Everything goes fine until the form is opened. The opened
form seems to freeze and does not refresh (no errors are
generated). However, If I output the received data to the
console it seems to be receiving data. The problem is
only with the UI.
Part of the code is included below (pls let me know if
you need the entire source code).


private void Receive()
{
try
{
if ( callbackProc == null )
callbackProc = new AsyncCallback(ReceiveCallback);

StateObject state=new StateObject();
state.workSocket = s;
s.BeginReceive(state.buffer, 0,
StateObject.BufferSize,
SocketFlags.None, callbackProc, state);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void ReceiveCallback (IAsyncResult ar)
{
try
{
StateObject state = (StateObject) ar.AsyncState;
int bytesRead = state.workSocket.EndReceive(ar);
string szData =
System.Text.Encoding.Unicode.GetString
(state.buffer, 0, bytesRead);

frmNew n = new frmNew();
n.show();

Receive();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
 
<!-- code deleted --->

Hi,
Just to update, I tried using Application.DoEvents()
together with ShowDialog(). That helped but only to a
certain extent. The form now does not freeze but the
callback in the Mainform is not receiving any messages
now.
Jesbin
 
Hi,
I'm sorry I forgot to update on the current status. I was able to
resolve the problem with the help of other fellow poster to the thread.
I used the .Invoke() method of a control in the parent for, which caused
the form to be invoked from the parent thread. This seems to have
resolved the problem.
Jesbin
 
Back
Top