简单的程序逻辑来连续调用方法

发布于 2024-09-05 19:53:50 字数 447 浏览 3 评论 0原文

我遇到的情况是,我的程序需要进行处理,然后等待一段时间,比如说 5 秒,然后再次进行相同的处理。

我不知道如何实现逻辑。

我开发了一个逻辑,代码如下:

private void ProcessEmail()
    {
        PprocessEmail:;

        //Do whatever you want

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

这段代码的作用: 我只需要调用这个方法一次,它就会进行处理,然后等待 5 秒,然后再次处理。

到目前为止,上面的代码工作正常,但我听说在编程中使用“goto”语句并不被认为是好的。

我想知道,这段代码是否有任何副作用,或者是否有其他有效的方法来做同样的事情。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2024-09-12 19:53:50

看看循环。 这篇 Wiki 文章可能是一个很好的理论起点。

如果是 C#,您将使用一个会永远循环的 while(true)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文