Search code examples
c#multithreadinginvokerequired

Finding methods that require InvokeRequired


I come to you to see if someone has an idea on how to solve a problem I've come across while doing a migration to ActiveMQ. I'm using ActiveMQ to send notifications within this project (in C#), and after finishing the implementation i found some errors concerning threading problems. ( I know that the solution for that exception is to use the "if this.InvokeRequired.... etc", but my question is:

Is there any way of finding all the methods that require this invoke?

Of course i could check step by step all the events triggered with the notifications, but, apart from the fact that it would take me too much time, it wouldn't solve me future programming errors.

I guess there is a better way to figure out this, but i cannot think of it right now. Have you encountered the problem before?

Thank you very much for your help


Solution

  • No. There is no automated way to do this, unless of course you've setup a test driven project from the beginning. In which case, you could add some conditions to test for thread correctness.

    Software cannot deduce what you intended, except in very specific ways (FxCop for instance, and the IDE's warnings about certain things). What you wrote is not necessarily what you meant to write. You're effectively asking for software that can figure out what you meant to do.

    The only way to know if an invoke is required is to know the context in which any given function operates. If it operates on a background thread, and you are calling code that needs to run on the main thread (such as GUI code) then an invoke is required.

    You have to figure that out yourself.