1

I am working on a soloution with lots of projects in it. I have a project that hosts my utility methods. All of the other projects are using this project methods.

I want to know is it ok that a static method gets called from multiple projects maybe at same time?

Also, in one of my methods I have an optional parameter to do a Thread.Sleep() what happens if one project calls this method with sleep and other one with no sleep at the same time?

I am really confused by this structure!

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • This answer should be helpful: http://stackoverflow.com/questions/3037637/c-sharp-what-if-a-static-method-is-called-from-multiple-threads – dash Nov 21 '12 at 12:50

2 Answers2

3

I want to know is it ok that a static method gets called from multiple projects maybe at same time?

Well it depends on what the method does. You need to take responsibility for making sure it's okay to call it from multiple threads at the same time. (Whether the callers are in the same project is almost always completely irrelevant.)

So long as you don't modify any shared state in a dangerous way - e.g. adding to a List<T> without any synchronization - you should be fine. It all depends on what you're doing though.

Each call will be independent in terms of having a separate set of parameters and local variables. Now if some of those parameters refer to objects which are visible to other threads, that's a different matter... but we can't tell that from your question.

Note that the use of Thread.Sleep here is pretty much irrelevant. Admittedly if you have some synchronization (e.g. via lock) then it would be pretty unpleasant for the thread which owns a lock to then sleep...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

As the function says, Thread Sleep, the current calling thread will sleep for the duration of the call. If you have several threads calling into this function, they will all sleep. If all your projects are run from the same thread, they will never call this function simultaneously, and will sleep in serial.

sphair
  • 1,674
  • 15
  • 29