Monday, April 28, 2014

What is proper way to prevent two separate threads from running at same time?

I have two functions, one is timer call back function, and the other is a function which is called by a running thread, it is not the thread start function itself, this second function is called by another thread.


(One is a timer thread)


If I hit timer_Elapsed function first, I want to block on DoStuff until function exits


If I hit DoStuff function first I want to block on timer_Elapsed until function exits


What is the proper as well as efficient way to do this.


private void timer_Elapsed(object sender, ElapsedEventArgs e)


{


...do timer stuff....


}


{thread started somewhere else....at some point calls DoStuff() }


void DoStuff()


{


...do my stuff...


}


No comments:

Post a Comment