PC Review


Reply
Thread Tools Rate Thread

async callback problem

 
 
jesbin
Guest
Posts: n/a
 
      12th Aug 2003
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);
}
}

 
Reply With Quote
 
 
 
 
feroze
Guest
Posts: n/a
 
      13th Aug 2003
Try doing Application.DoEvents() to make sure that message pump is running.

"jesbin" <(E-Mail Removed)> wrote in message
news:0dca01c360d2$e3be8b20$(E-Mail Removed)...
> 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

>



 
Reply With Quote
 
jesbin
Guest
Posts: n/a
 
      13th Aug 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
 
Reply With Quote
 
Parker Zhang [MSFT]
Guest
Posts: n/a
 
      15th Aug 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.

 
Reply With Quote
 
Jesbin Baidya
Guest
Posts: n/a
 
      15th Aug 2003
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



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with async callback billsahiker@yahoo.com Microsoft C# .NET 2 2nd Nov 2007 09:11 PM
Problem with async call and callback procedure ITALstudio s.r.l. Microsoft VB .NET 2 15th Oct 2004 01:35 PM
Raising an exception in a Sockets Async Callback Problem. David Microsoft C# .NET 3 17th Feb 2004 10:13 PM
RE: async callback problem Ying-Shen Yu Microsoft Dot NET Framework Forms 3 13th Aug 2003 04:10 PM
Re: async callback problem Stephen Alpert Microsoft Dot NET Framework Forms 0 12th Aug 2003 05:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:17 PM.