fernandos fernandos
fernandos fernandos

Reputation: 47

Calling method inside timers timer Elapsedevent

I want to know is it always required to put lock keyword when we are calling method inside timers elapsed to achieve thread safety of that method.

I have a code that is not using any shared variable and calling inside Elapsedevent. Do I need to lock it or it will be fine since the threads are having their own stack.

Here is my Elapsedevent code,

void t_Elapsed(object sender, ElapsedEventArgs e)
{
    Test t = new Test();
    t.Process();
}    

class Test
{
    public void Process()
    {    
        // do ingsome processing using only local variables.
    }
}

Upvotes: 1

Views: 94

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73492

If a method which doesn't uses shared state, that is inherently thread safe. You don't need a lock at all.

Upvotes: 1

Related Questions