timer reset

I am looking for a solution to my problem.

What i am working on at the moment is a function that will reset timer each time its called.
So basically when i call timer function it will reset current run time to 0 and restart it self.

If timer will run for 10 seconds (for example) an event will be triggered.

I will call this funtion from a loop.


while (!Condition)
{
      //executing some code
      TimerReset();
}


So how do i achieve that?
How do you reset a timer?

Some code example how its done from a while loop would be great.
andy andrewAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Najam UddinCommented:
Although I am unable think about why do you want to do it, yet try to stop and immediately start timer

timer.Stop();
timer.Start();

Open in new window

andy andrewAuthor Commented:
I have an application that connects to a server.
This is my solution to check if i still have a connection.

In while loop i will check if i got any packets from server in last 10 seconds... and each time loop iterates i will set timer to zero.
So if timer reaches his elapsed time i have been disconnected.

PS i have tried various other solutions to check if i am still connected to the server but non of them have worket or was 100% reliable.
AndyAinscowFreelance programmer / ConsultantCommented:
I'm not getting something.  Your code does what you ask for.  What is not working with your code.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

andy andrewAuthor Commented:
Well i failed to mention i am working in C# win forms application.

timer.Stop();
timer.Start();

Open in new window


Above solution does not work. What happens is, timer restarts from the time when you pressed Stop.

Is there a way to just dispose timer instance each time one loop cycle is done and create new object? Is that a viable solution?

Like this:

timer1.Dispose();
timer1 = new System.Windows.Forms.Timer();

Open in new window

Najam UddinCommented:
you don't have to press stop, just write those line in elapsed event

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
andy andrewAuthor Commented:
Thank you!
Jacques Bourgeois (James Burger)PresidentCommented:
Depending on what you mean by "server", the classes that you use might have an event that is triggered whenever the connection is broken, so you might simply have to subscribe to that event. This would be a far better solution than a continuously running loop.

This is the case for instance for the SqlConnection class, that has both a StateChanged and an InfoMessage events that will be called when the connection switch from Open to Close or when there is a problem on the connection.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.