0

I've seen a few questions here about begininvoke and fire and forget and such and also a question about endinvoke if it has to follow a begininvoke or not. For more on the last part here: Must every BeginInvoke be followed by an EndInvoke?

My question is strongly related to that and still a bit more: I want to create a fire and forget invoke (I don't care there if the invoke finishes or not as the part calling the invoke is not able to handle this at all also as the invoked part starts a dialog it is up to the user when that invoke ends and not up to the program (except application exits).

So my question is now here: Is there any difference between the two options using startinvoke without endinvoke or using a threaded invoke?

Startinvoke without endinvoke:

mainForm.BeginInvoke((MethodInvoker)delegate { mainForm.DoSomething(); });

Threaded invoke:

Thread t = new Thread(() => mainForm.Invoke((MethodInvoker)delegate { mainForm.DoSomething(); }));
t.IsBackground = true;
t.Start();
Community
  • 1
  • 1
Thomas
  • 2,886
  • 3
  • 34
  • 78
  • 1
    There's no conceivable reason that I can think of to ever use a Thread like that. It is far too expensive and *much* harder to diagnose when the invoked code throws an exception. – Hans Passant Jul 29 '14 at 07:24
  • Like I said its mostly for fire & forget invocations. The thread method was an idea I had when I read that it is adviced that a begininvoke is always to be followed by an endinvoke (also exceptions in the invoked method are not of a concern in this part of the code (else it would complicate matters for that programpart quite a bit). So a difference is the cost (that startinvoke is better in terms of cost). are there any other differences? or can they be used interhchangingly (costs aside)? – Thomas Jul 29 '14 at 07:34
  • What you read is **not** true for Control.BeginInvoke(). That method has nothing whatsoever to do with a delegate's BeginInvoke() method. [Read this](http://stackoverflow.com/a/18019496/17034). – Hans Passant Jul 29 '14 at 07:42
  • AH ok that explains a lot there. I fell there for the same naming of the invoke methods for delegates and controls. then in essence I can use begininvoke without an endinvoke there without (additional) problems caused by that. thanks a lot there – Thomas Jul 29 '14 at 07:50

0 Answers0