John’s console application calls my DLL function many times (~15x per sec). I am thinking to put this function as a static method.
I know that :
- It can only access static props and objects.
- It doesn’t need an instance to run the function.
But I don’t know if these are the only questions which i need to ask myself.
Each John’s calls to my function is in a new thread that he creates.
- If there is an error in my function, how will this affect all other calls?
- Should I make this function a regular function with instance to the class (which John will create)?
- What about GC?
What is the best practice answer for this question?
Sounds like there could be a problem. Calling a method which operates on static (shared) objects in a multithread environment should ring some alert bells for you.
Review your code and if there’s a chance that a shared object is accessed from two (or more) threads at the same time, make the object an instance field and make your methods instance methods.
Of course, whether or not there is a risk depends much on the actual code (which you don’t show) but making all calls nonstatic means that you lower the potential risk.