Asynchronous Invoking - Is EndInvoke required? [duplicate]
Asked Answered
R

3

7

Possible Duplicates:
Must every BeginInvoke be followed by an EndInvoke?
Is EndInvoke() optional, sort-of optional, or definitely not optional?

I've got a multithreaded application, and one of the secondary threads needs to get some code to execute on the main thread once every couple of minutes. There isn't any return value, and the second thread doesn't care if it raises any exceptions or fails to run.

So far, I've been getting it to run the code via Form.Invoke, but it sometimes takes much longer than usual (a couple of seconds) and blocks the thread until it completes. I need the second thread to be able to continue execution without stalling for several seconds.

BeginInvoke sounds like it'd do the job nicely, but I don't really have anywhere to call EndInvoke, since I don't want to wait for it or get a return value. And considering that the code being invoked involves a bunch of native calls, I'm not sure if its a good idea to not EndInvoke.

Do I need to call EndInvoke at all, or is there some other way of getting code to run on the main form thread asyncronously that I should be using instead?

Thanks =)

Rivas answered 30/6, 2011 at 4:56 Comment(0)
W
7

You can call EndInvoke to retrieve the return value from the delegate, if neccesary, but this is not required. EndInvoke will block until the return value can be retrieved.

Source:http://msdn.microsoft.com/en-us/library/0b1bf3y3.aspx under Remarks

Waxen answered 30/6, 2011 at 5:16 Comment(1)
Awesome, so I can just change my Invokes to BeginInvokes without blowing anything up.Rivas
C
5

One typical way to call EndInvoke is by including a completion callback with the 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, the End may be required for cleanup and will also typically (re)throw any Exception that occurred during the process.

Carat answered 30/6, 2011 at 5:4 Comment(0)
C
3

You should ensure that EndInvoke is called, but you can do it pretty easily, something like:

  var action = new Action(SomeMethodGroup); 

  action.BeginInvoke(new AsyncCallback(

        x => (x.AsyncState as Action).EndInvoke(x)), action); 
Crouton answered 30/6, 2011 at 5:3 Comment(3)
I believe the OP is talking about Form.BeginInvoke in particular and not the Begin/EndInvoke pattern in general. Looking at MSDN, Form.BeginInvoke doesn't seem to follow the Begin/EndInvoke pattern exactly.Mononucleosis
@dtb, In fact, I don't believe that it's strictly necessary for Form.BeginInvoke or Delegate.BeginInvoke, as I believe it only creates a WaitHandle if you try to wait (i.e. call EndInvoke.) The WaitHandle needs to be cleaned up, but it will be if you've called EndInvoke.Carat
Good point, maybe you don't need EndInvoke, maybe a simple method that might work as a 'fire and forget' strategy?Crouton

© 2022 - 2024 — McMap. All rights reserved.