About sockets. Receive methods

D

Diego F.

Hello. I'm using the socket class in an application and I use socket.receive
to wait for data. The problem is that the interface gets locked while the
receive method is executing.

I want to see the interface so the user knows what's happening at any time.

Is that possible?
 
M

Mike Spike

You should put the socket code in another thread.

That way your GUI thread will be free to repaint and update while you are
looping / waiting to read from the socket.

I have not used sockets in dot net but that kinda a general rule to keep
things that loop alot off the GUI thread. Depending on how the sockets are
blocking or non blocking makes a diffrence but you might be able to put
application.doevents or ProcessMessagess or what ever makes the application
yeild to process windows messages. if you put that within your loop it
should help, but its a hack rather than a fix.

Mike.
 
T

Tom Shelton

Hello. I'm using the socket class in an application and I use socket.receive
to wait for data. The problem is that the interface gets locked while the
receive method is executing.

I want to see the interface so the user knows what's happening at any time.

Is that possible?

--

Regards,

Diego F.

Diego,

Recieve is a synchronous socket operation, therefore it will block
until it recieves data or one of the endpoints is disconnected. You
have two choices if you don't like that behavior.

1) Manually spin off a separate thread of execution to perform you
socket operations.

2) Use the BeginXXX socket methods for asynchronous behavior.

IMHO, #2 is the far superior choice. You can find more information
about general usage of sockets in .net here:

http://msdn2.microsoft.com/en-us/library/b6xa24z5.aspx

Here is a specific article on asynchronous client sockets:

http://msdn2.microsoft.com/en-us/library/bbx2eya8.aspx

HTH
 
D

Diego F.

Thank you for your responses. I tried using a second thread, but then I
can't access the UI controls (I get an execution exception), so I'll try the
BeginAccept solution.
 
D

Diego F.

I'm thinking about the asynchronous solution, but, it will continue the code
in the main thread. I'm not sure that it works for me. The code after the
Receive, is for managing the sent data.

Do you see my problem? I'm migrating an old VB6 application, and it uses the
DataArrival event.
 
T

Tom Shelton

I'm thinking about the asynchronous solution, but, it will continue the code
in the main thread. I'm not sure that it works for me. The code after the
Receive, is for managing the sent data.

Do you see my problem? I'm migrating an old VB6 application, and it uses the
DataArrival event.

--

Regards,

Diego F.







- Show quoted text -

The Async methods are based on callbacks, so it is going to be similar
to the way it works with the dataarival event, but with a little more
housekeeping.

By the way, either way - manual thread creation or the BeginXXX
methods, you will be unable to directly access the UI. Both methods
cause a thread creation, it's just using the async methods take care
of it and are very effiecient. So, you may want to look at this
series of articles on windows forms and multithreading:

http://msdn2.microsoft.com/en-us/library/ms951089.aspx

That's a link to the first part - but it is a 3 part article, so read
it all :)

The other option is to use the sync method, and use a backgroundworker
component... Or find a 3rd party component.
 
M

Michael M.

In terms of accessing the GUI from network threads i some times do this :

Hook the windows with the controls then when I want to update the GUI is use
POSTMESSAGE

This way the message is sent to the Window Loop which is hosting the GUI
controls, the Loop is running inside the GUI thread anyway so no access
violation occurs (accessing the controls via proxy).

If it is a complex control you can send your own message constant to the
main window loop like POSTMESSAGE(form1.hwnd, WM_LogConnection_Request, 0,
PointerToString)

then process this message

if Msg.Message = WM_LogConnection_Request then
ListBox1.Item.Add(Translated_String_from_pointer)

end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top