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);
}
}

Re: async callback problem by feroze

feroze
Tue Aug 12 22:40:13 CDT 2003

Try doing Application.DoEvents() to make sure that message pump is running.

"jesbin" <jesbin@yahoo.com> wrote in message
news:0dca01c360d2$e3be8b20$a401280a@phx.gbl...
> Bruce,
> Thank you for such a quick reply.
>
> The callback aspect of the application is working fine
> because if I test it independently (without creating the
> form), it seems to be working. The Framework doc states
> that once the EndReceive() is done, I need to reissue
> BeginReceive() to wait for more messages.
>
> The only funny part is the creation of form. The STA
> model does seem logical but I still cannot understand why
> I can still see the components of the created form using
> the Debugger and infact I can even update the contents of
> the controls but the form looks like it is not
> responding. If I output the received message using
> Console.WriteLine() it seeems to be ok
>
> Jesbin
>
>
> >-----Original Message-----
> >In your example, you are calling Receive from within the
> callback method. If
> >this is not a typo, then it is certainly a problem - you
> should be calling
> >the EndReceive method associated with the BeginReceive
> that you originally
> >passed the callback delegate to.
> >
> >In addition, from the Framework help:
> >
> ><excerpt>
> >Windows Forms uses the single-threaded apartment (STA)
> model because Windows
> >Forms is based on native Win32 windows that are
> inherently apartment
> >threaded. The STA model implies that a window can be
> created on any thread,
> >but it cannot switch threads once created, and all
> function calls to it must
> >occur on its creation thread. Outside Windows Forms,
> classes in the .NET
> >Framework use the free threading model. For information
> about threading in
> >the .NET Framework, see Threading.
> >Windows user-interface only operate on the application
> main thread
> ></excerpt>
> >
> >In theory, you should be able to create a new form on a
> new thread - however
> >if you are interacting with any control (such as another
> form) created on
> >another thread, then you will need to use its Invoke
> method (see the help).
> >
> >Regards,
> >
> >Bryce Marshall
>



Re: async callback problem by jesbin

jesbin
Wed Aug 13 01:59:26 CDT 2003

<!-- 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

RE: async callback problem by parzhang

parzhang
Fri Aug 15 02:23:24 CDT 2003

Hello,

Would you please upload the entire source code?

Thank you.

--
Parker Zhang
Microsoft Developer Support

This posting is provided "AS IS" with no warranties, and confers no rights.