• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1780
  • Last Modified:

How to cancel a pending BeginReceive method of Socket class in .NET without using Close method ?

How to cancel a pending BeginReceive method of Socket class in .NET without using Close method ?
0
kalibuk
Asked:
kalibuk
  • 5
  • 3
1 Solution
 
kalibukAuthor Commented:
Any help appreciated.
0
 
MajorBigDealCommented:
Hi Kalibuk,  do you any code written yet? Can you post it?  Is this a general inquiry or do you have a specific situation or bug that you are trying overcome?
0
 
kalibukAuthor Commented:
From my code I call attached function WaitForData(). When the caller decides to not wait any more, the callback OnDataReceived is still hanging in memory. Proper solution is to call Close() method on ((Socket)objResource), but I cannot lost handle to the opened socket or create another one.
private int WaitForData()
        {
            try
            {
                if (pfnCallBack == null)
                    pfnCallBack = new AsyncCallback(OnDataReceived);
                // now start to listen for any data...
                m_asynResult =
                ((Socket)objResource).BeginReceive(m_DataBuffer, 0, m_DataBuffer.Length, SocketFlags.None, pfnCallBack, null);
                return NTRESULT.OK;
            }
...
}

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MajorBigDealCommented:
Just asking so I can understand the situation better:

1. What is the reason you don't want to specify the timeout?

2. What is the reason you can't create another socket?
0
 
MajorBigDealCommented:
It seems that the short answer is that you can't:

http://stackoverflow.com/questions/563593/cancelling-a-socket-xxxxasync-call

However, there might be a slightly different way to accomplish your goal. That's why I'm asking the above question.  Just trying to understand what you are trying to do.

I suppose you could abort the thread but I would not design it that way.  Another choice would be to use a non-blocking socket instead of async:

http://stackoverflow.com/questions/5743304/non-blocking-udp-socket-net-a-non-blocking-socket-operation-could-not-be-compl
0
 
MajorBigDealCommented:
Please disregard the comment about aborting the thread, that won't help and is a very bad idea.
0
 
kalibukAuthor Commented:
Hallo,
so as you suggested we abandoned idea of async receive while there is no easy way to kill pending method. I rewrote the code to use simple Receive method in blocking mode  and now I'm able to cancel Receive method on custom event I get from caller. So thanks!
0
 
MajorBigDealCommented:
Glad I was able to assist - and thanks for asking such an interesting question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now