I'm new and I am trying to figure something out. I have a simple program which executes a method either when an event happens or when my timer ticks. Lets just say the method takes 5 seconds to complete. During that 5 seconds an event could happen. What would I use/do to make sure my method isn't called while its already running.
Or is this something I would not have to worry about in a simple program, as it wouldn't run until the method is done anyway ?
A little background and example. I am using System.Windows.Forms.Timer, I am not using any threads or anything, and I just have a simple windows form application with a timer, a method to do stuff, and two event handlers.
Program{
public Program(){
DoSomething();
Event.EventHappened += HandleEvent;
}
void DoSomething(){
MyTimerVariable.Stop();
InitMyTimer();
//Just do something check network, access db write line, something
//that in this example takes 5 secs for example purposes
MyTimerVariable.Start();
}
void HandleEvent(){
DoSomething();
}
void InitMyTimer(){
// Create Timer, and add Timer tick event
}
void TimerTickedEvent(){
DoSomething();
}
}
I left a few things out of that program, but just was trying to show how simple it is. I was just thinking about making it more efficient so that say in the middle of doing "DoSomething()" an event is triggered, I wouldn't want two "DoSomething()"s method running.