2

I'm trying to unsubscribe from MessageReceived on the bound UDP socket (DatagramSocket) and getting the exception. That is how I register:

    EventRegistrationToken myRegToken;
    myRegToken = (mySocket->MessageReceived += ref new TypedEventHandler<DatagramSocket^, 
          DatagramSocketMessageReceivedEventArgs^>(listenerContext, &ListenerContext::OnMessage));

This is how I unregister:

mySocket->MessageReceived -= myRegToken;

And I get an Exception: A method was called at an unexpected time.

What I'm doing wrong? Thanks.

Rafi
  • 161
  • 5

2 Answers2

0

No luck, the only solution seems to delete mySocket with mySocket = nullptr; and recreate it if needed

Poppyto
  • 554
  • 8
  • 17
  • Please, note that DatagramSocket is IDisposable so you should call Dispose before mySocket = nullptr – norekhov Dec 10 '14 at 10:37
0

Just use

Socket.Dispose();

It's the official way, described here:

https://msdn.microsoft.com/library/windows/apps/br241358

Jmie
  • 97
  • 1
  • 12