Solved

Does AsyncCallback start a new thread?

Posted on 2004-10-20
3
413 Views
Last Modified: 2011-10-03
Hi!
I need help with a problem i am having...

I have made a simple program that starts and create a thread that makes a connection to a server.
What i want to do is have my mainthread doing some gui-stuff, and a socket-thread that handles all the socket-stuff.
So i made a class that have a function called Start that makes a connection, and then sets a asynccallback:

Start:
      m_sock.Blocking = false;
      AsyncCallback onconnect = new AsyncCallback( OnConnect );
      m_sock.BeginConnect( epServer, onconnect, m_sock );

then when it runs OnConnect:
      AsyncCallback Receivedata = new AsyncCallback( OnReceivedData );
      sock.BeginReceive( m_byBuff, 0, m_byBuff.Length, SocketFlags.None, Receivedata, sock );


What i was thinking is that the socket thread that starts with this funtion should only handle the socket stuff.
But when i run my program and the socket is set up my socket-thread dies beacuse the function has ended.

But the funny thing is that i can still receive data on my socket?
When i make these AsyncCallbacks and they gets triggered, do they start their own thread?

So my question is this, since my program is running and can handle incoming data eventhough my thread that started the socket has died...which thread is handling my OnReceivedData???

Any help and/or remarks will be much appreciated!

Thanks!
/Dj
Thanks for you help!
0
Comment
Question by:iamdj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 350 total points
ID: 12357931
When you use .NET sockets in asynchronous mode, you don't need to create a worker thread. .NET takes care about creating additional threads for sockets and notifies your program when some socket events are raised.
Download file CSharp.msi from here:
http://www.microsoft.com/downloads/details.aspx?familyid=08e3d5f8-033d-420b-a3b1-3074505c03f3&displaylang=en

and read asynchronous sockets sample. It contains all information to build socket server and client applications.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12358031
The answer is simple - create non-blocking socket in the main thread.
Your worker thread dies because thread function exits. You can receive data from socket because socket runs in it's own thread.

Interesting question is whether callback functions are called in the context of main thread or socket worker thread. You can test this by calling Thread.CurrentThread.GetHashCode from the main thread and callback and comparing results.
0
 

Author Comment

by:iamdj
ID: 12358062
Thanks a lot for the quick and really good help!

/Dj
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question