This method adds something to db every 5 seconds.
I want to stop this process after 10 products added.
How can i stop this process?
public static void AddMyProductToDB()
{
Timer myTimer = new Timer(5000);
myTimer.Start();
if (!CountControl())
{
myTimer.Stop();
myTimer.Enabled = false;
myTimer.Dispose();
}
else
{
myTimer.Elapsed += new ElapsedEventHandler(MyWork);
while (true) { }
}
}
You have infinite loop which will block the code from being executed for the second time.
Also you seems to have to read this tutorial
http://www.dotnetperls.com/timer
OR use this code snippet