我应该在C#中有许多调用同一个FUNC使用静态函数? [英] Should I use static function in c# where many calls the same func?
问题描述
约翰的控制台应用程序调用我的DLL函数多次(〜每秒15倍)。我想到把这个函数作为的静态的方法。
John's console application calls my DLL function many times (~15x per sec). I am thinking to put this function as a static method.
我知道:
- 只能访问静态的道具和物品。
- 在它并不需要一个实例来运行的功能。
但我不知道这些都是我需要问自己的唯一问题。
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.
- 如果有错误在我的功能,这将如何影响所有其他电话?
- 我应该让这个函数实例常规函数的类(约翰将创建)?
- 在什么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.
查看您的code,如果有一个共享的对象是从两个(或更多)线程访问在同一时间一个偶然的机会,使对象的实例字段,让你的方法实例方法。
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.
当然,无论是否存在风险取决于你降低潜在的风险太大实际code(你不显示),但让所有的呼叫非静态的手段。
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.
这篇关于我应该在C#中有许多调用同一个FUNC使用静态函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!