Asynchronous call - is EndInvoke required?

Possible duplicates:
Should every BeginInvoke follow EndInvoke?
Is EndInvoke () optional, sorting optional or definitely optional?

I have a multi-threaded application, and one of the minor threads should get some code to execute on the main thread every couple of minutes. There is no return value, and the second thread does not care that it throws any exceptions or does not start.

So far, I have run it to run the code through Form.Invoke , but sometimes it takes much longer than usual (a couple of seconds) and blocks the thread until it completes. I need a second thread to continue execution without stopping for a few seconds.

BeginInvoke sounds like it did the job beautifully, but I really have nothing to call EndInvoke , since I don't want to wait for it or get the return value. And given that the code being called includes a bunch of native calls, I'm not sure if this is a good idea not EndInvoke .

Do I need to call EndInvoke at all, or is there some other way to get the code to run the stream in the main form asynchronously, what should I use instead?

Thanks =)

+6
source share
3 answers

You can call EndInvoke to get the return value from the delegate , if necessary, but not required. EndInvoke will block until a return value is received.

Source: http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx in the Notes section

+6
source

One typical way to call EndInvoke is to enable the completion callback with BeginInvoke so that you can call EndInvoke in the callback. This is more important for Begin / End methods that do something more specific than Invoke, such as BeginRead / EndRead; in the latter cases, End may be required to clear, and, as a rule, (re) throw any exception that occurred during the process.

+4
source

You need to make sure that EndInvoke is called, but you can do it quite easily, something like:

  var action = new Action(SomeMethodGroup); action.BeginInvoke(new AsyncCallback( x => (x.AsyncState as Action).EndInvoke(x)), action); 
+3
source

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


All Articles