10

I'm building a rather simple application that performs a few seperate HTTPWebRequests, each on their own timer. It's not likely I will need to have more than 3 seperate requests running at a time.

If a Timer Tick fires while another HTTPWebRequest is in progress, what happens to the event fired by the Timer Tick? Should I add Threading?

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
Mr Wednesday
  • 552
  • 4
  • 16
  • 1
    depending on the timer you use you already might have multithreading (most certainly) - in any case you can have more than one HTTP-Request at a time (depends on system-settings - I think it's 8 or something) – Random Dev Mar 13 '12 at 22:23
  • Have you thought about making async HttpWebRequest's? All the benefit without all the headache of threads. – jb. Mar 13 '12 at 22:37

2 Answers2

6

The answer is almost always - no, don't use threads just because you can.

Consider making asynchronous calls first as it is easier to write correct code for. It is likely more efficient use of resources (as threads are not unlimited resource) if you need additional arguments.

Links:

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • Agreed. Especially with C# 5.0, asynchronous calls are a breeze. – stan Mar 13 '12 at 22:38
  • Would a long/hanging Async call block the UI? – Mr Wednesday Mar 14 '12 at 00:22
  • No, call returns "immediately" and your callback/event handler will simply not be called till response is received/timed out. You are already doing "asynchronous processing" for Timer events, async HttpRequest would be similar. – Alexei Levenkov Mar 14 '12 at 00:43
  • Thanks Alexei, I'm going to go with async calls. I've already found a good starting point in another question asked on SO. The exact question you've linked. – Mr Wednesday Mar 14 '12 at 00:49
1

When Timer.Tick fires it's handler will be scheduled for execution in Thread Pool and most likely, executed in another thread.

the_joric
  • 11,986
  • 6
  • 36
  • 57