Is it always using Form.Invoke () bad practice?

I know this might be a very simple question, but I couldn't be sure. I have this in a module:

Public theHandle As IntPtr

      

And this is in my main form named Form1:

Private Sub Form1_HandleCreated(sender As Object, e As System.EventArgs) Handles Me.HandleCreated
    theHandle = Me.Handle
End Sub

      

I have many other classes, modules and threads and without using InvokeRequired

, I use this to call delegates from all sides. I mean from other threads, classes, modules, etc.

DirectCast(Form1.FromHandle(theHandle), Form1).Invoke(D_Calculate)

      

instead:

D_Calculate.Invoke()

      

Is this bad practice? Is there really a purpose to check InvokeRequired

every time?

+3


source to share


1 answer


You only need to check InvokeRequired

where you are multithreading and you need to update the updates to the UI thread.

I would say that using it globally is probably not a good idea because you have to define and understand your threading model and therefore know exactly where the thread boundaries are and the points that need this check.



If you hang them literally everywhere, you basically say "I have no idea if this will only be caused by the UI thread or not" and (if you do have multiple threads) will lead to a debug world which results in you find you missed a dot or other non-thread safe code.

+6


source







All Articles