My windows app's requirement are:
Using HttpWebRequest get web request/response every 3 seconds in one thread.(total is about 10 threads for doing this web request/response.)
Each thread use some global variables.
I want to use a System.Timers.Timer and async and await. But I don't know that is a best way for high performance. And then how to test them. I am a green in C#.
You could write a
RepeatActionEvery()
method as follows.It's parameters are:
action
- The action you want to repeat every so often.interval
- The delay interval between callingaction()
.cancellationToken
- A token you use to cancel the loop.Here's a compilable console application that demonstrates how you can call it. For an ASP application you would call it from an appropriate place.
Note that you need a way to cancel the loop, which is why I pass a
CancellationToken
toRepeatActionEvery()
. In this sample, I use a cancellation source which automatically cancels after 8 seconds. You would probably have to provide a cancellation source for which some other code called.Cancel()
at the appropriate time. See here for more details.