views:

189

answers:

2

I have a method name "LogClick" that is called 2 or more times per second. I call it in the tradicional way:

Call New MyClass().LogClick()

Everty time I call "LogClick", the "MyClass" is instanciated. So I think: "why not transform this method to shared?"

Public Shared Sub LogClick()
...
End Sub

MyClass.LogClick()

Will I have performance, concurrence ou memory problems transforming this method to shared?

A: 

]Hey,

No performance issues per se, but any variables that are static as well will be retained in memory, even in ASP.NET. So you have to account for that. But generally, if there aren't any static variables, performance will be the same as an instance... depending on what's in the instance it may be better with a static method.

HTH.

Brian
A: 

Shared (static in C#) is inherently non-thread safe, as all your page instances will be accessing the same object.

"Problems" entirely depends on what LogClick() is doing. You'll have to be very careful to analyze the code, assuming that execution will transfer to a different thread after every line is executed, and come back afterwards.

womp
The LogClick() inserts 1 row in a table on database. So, it makes 2-more inserts per second.Another thing, de Insert() is shared too, may I have any problems with that?
Fernando
Depends on how you're creating and destroying your database connections, and your connection properties. If any of those actual variables are shared, you'll be in trouble.
womp
Yes, I realized that.. thanks!
Fernando