views:

281

answers:

2

Dear all, 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

+1  A: 

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.

Mystere Man
Ok, i was afraid there were no automated ways of doing it. i guess I just need to go through the code then :)Thank you for your help! At least i will not be looking for something that doesn't exists!
Srodriguez
Since you're new here, you should consider clicking the checkmark next to the answer you consider to help you the most. I'm not saying it should be me.. not unless you think so. ;)
Mystere Man
A: 

Its not that certain methods you are calling require the invoke. It depends on what thread you are calling those methods from.

If you're calling a method in a Winforms app, on a thread other the UI thread, it's going to require the Invoke.

Depending on the code, it should be easy to analyse what calls are made from which threads, especially if you are naming background threads (which always comes in handy). But there's probably not an automatic way to do this - just step back and look at your code.

Dan