0

I am in a situation, in which my program needs to do processing and then wait for some interval, let's say 5 seconds and the do the same processing again.

I don't know how to implement the logic.

I have developed a logic, the code is below:

private void ProcessEmail()
    {
        PprocessEmail:;

        //Do whatever you want

        System.Threading.Thread.Sleep(5000);
        goto ProcessEmail;
    }

What this code does: I only have to call this method once, it will do the processing then wait for 5 seconds and then again process.

So far above code is working fine, but i have heard using "goto" statements is not considered good in programming.

I want to know, will there be any side effect of this code or is there any other efficient way of doing the same thing.

NCCSBIM071
  • 1,207
  • 1
  • 16
  • 30
  • You are aware of the existence of control-flow structures like "while" and "for"? – zerm Jun 10 '10 at 10:03
  • Hi, guys thanks for the reply, i do know about the loop. But is there any other efficient method than looping and as i have done. You understand the kind of functionality i want. – NCCSBIM071 Jun 10 '10 at 10:19
  • There is recursion, but i guess you do not want that in this case. What's wrong with loops? – zerm Jun 10 '10 at 10:21
  • Yes you are right, i will implement while, I thought about recursion but recursion will consume lot more memory than using while, since i have to implement this in a window service which runs continuously foreever. Ain't i right about recursion, what do you say zerm? – NCCSBIM071 Jun 10 '10 at 10:33
  • Every time you call a function you'll use up some space on the call stack, so if you do infinite recursion, sooner or later you'll run out of stack space and end up with a stack overflow :) – Hans Olsson Jun 10 '10 at 13:05

1 Answers1

2

Look at loops. This Wiki article might be a good place to start for the theory.

If it's C#, what you'd use is a while(true) that would loop forever.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • Hi, guys thanks for the reply, i do know about the loop. But is there any other efficient method than looping and as i have done. You understand the kind of functionality i want. – NCCSBIM071 Jun 10 '10 at 10:17
  • @nccsbim071: I'm slightly confused, the functionality you want is looping. The goto is just doing it in an uglier way. I don't know, but it wouldn't surprise me if internally the loop and the goto might be represented exactly the same way and I can't imagine that there would be a more efficient way of looping than via a loop of some kind... – Hans Olsson Jun 10 '10 at 10:25