Do I need to call EndInvoke in a callback from EventHandler.BeginInvoke (C # .Net 3.5)

I am setting up a WCF client and server script that should be reliable and scalable, and as part of this I am trying to make it multithreaded. Admittedly, I don't have much experience with multithreading.

On the client, a callback from the server (in this case, “OnMessage”) triggers a client-side event that should be sent in the background. I have a CallbackBehavior ConcurrencyMode client set to Single (at least for now). Therefore, to answer the callback, instead of calling the handler as usual (either the handler (sender, eventargs, or handler.Invoke ...), I call the .BeginInvoke handler.

Everything works fine, but in the callback, I wonder if I need to explicitly call EndInvoke or if I basically can not do anything (there is no lack of multi-threaded experience).

public void OnMessage(Message message)
{
    EventHandler<MessageEventArgs> handler = OnServerMessage;
    if (handler != null)
        handler.BeginInvoke(this, new MessageEventArgs(message), CompleteHandler, handler);
}

public void CompleteHandler(IAsyncResult result)
{
    ((EventHandler<MessageEventArgs>)result.AsyncState).EndInvoke(result);
}

Is it possible to replace CompleteHandler with a direct reference to the .EndInvoke handler or an empty method, or maybe there is something more that I should do?

In addition, at the moment, the subscriber blocks waiting for user input from the console. When the application is completed, it either blocks the wait in the database, or it will be asynchronous from the bottom up (this may not be necessary, but I would like to know anyway).

+3
source share
3 answers

, . , . , , 10 .

+8

BeginInvoke , EndInvoke . : , , EndInvoke, .

(, , BeginInvoke , ( , Dispatcher), EndInvoke, . , , .)

+1

CompleteHandler try/catch, , EndInvoke. "", - , , , / .

IAsyncResult : IASyncResult.AsyncState , - .

+1

Source: https://habr.com/ru/post/1774996/


All Articles