如何在 C#.NET 中进行事件驱动编程?

发布于 2024-09-08 15:05:49 字数 1442 浏览 0 评论 0原文

如何制作一个事件驱动程序,每当引发和处理事件时,主流程的执行都会暂停,直到事件处理程序完成?

我创建了以下程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace EventTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer t = new Timer(2000);
            t.Enabled = true;
            t.Elapsed += new ElapsedEventHandler(TimerHandler);
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine(i);
            }
        }
        public static void TimerHandler(object source, EventArgs e)
        {
            Console.WriteLine("Event started");
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine("Event "+i);
            }
            Console.WriteLine("Event finished");
        }

    }
}

main 方法应该打印连续的数字,并且每 2 秒触发一个事件。在事件处理期间,程序应该打印“事件开始”、“事件1”到“事件5”,然后“事件完成”,然后返回。

我期望 Main() 流程在调用 TimerHandler() 时停止,但是我的输出表明 Main() 和 TimerHandler() 两者同时运行

0
1
2
3
4
5
6
7
8
9
10
11
Event started
12
Event 0
13
Event 1
14
Event 2
15
Event 3
16
Event 4
Event finished
17
18
19
20
21
22
23
Event started
24
Event 0
25
Event 1
26
Event 2
27
Event 3
28
Event 4
Event finished
29
30
31
32
33

是否可以使 Main() 方法在处理事件时停止?

How do you make a event-driven program where the execution of the main flow is suspended whenever an event is raised and handled, until the event handler finishes?

I created the following program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace EventTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Timer t = new Timer(2000);
            t.Enabled = true;
            t.Elapsed += new ElapsedEventHandler(TimerHandler);
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine(i);
            }
        }
        public static void TimerHandler(object source, EventArgs e)
        {
            Console.WriteLine("Event started");
            for (int i = 0; i < 5; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                Console.WriteLine("Event "+i);
            }
            Console.WriteLine("Event finished");
        }

    }
}

The main method is supposed to print sequential numbers, and an event is fired every 2 seconds. During the event handling, the program is supposed to print "Event started", "Event 1" to "Event 5", then "Event Finished", then returns.

I expected Main() flow to stop while TimerHandler() is called, however my output suggests that Main() and TimerHandler() both are run simultaneously

0
1
2
3
4
5
6
7
8
9
10
11
Event started
12
Event 0
13
Event 1
14
Event 2
15
Event 3
16
Event 4
Event finished
17
18
19
20
21
22
23
Event started
24
Event 0
25
Event 1
26
Event 2
27
Event 3
28
Event 4
Event finished
29
30
31
32
33

Is it possible to make the Main() method to stop while the event is handled?

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

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

发布评论

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

评论(1

冰雪之触 2024-09-15 15:05:49

您正在使用 System.Timers.Timer,它在后台线程上处理刻度,通常使用线程池。

因此,是的,它将与您的主代码的其余部分同时运行。

至于你的第二个问题,有可能让 Main 停止,我需要知道预期的行为是什么。

如果您希望 TimerHandler 中的整个滴答一起运行,并在执行此操作时暂停 Main,则一个简单的锁就足够了:

namespace EventTest
{
    class Program
    {
        static object _Lock = new object();

        static void Main(string[] args)
        {
            Timer t = new Timer(2000);
            t.Enabled = true;
            t.Elapsed += new ElapsedEventHandler(TimerHandler);
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                lock (_Lock)
                    Console.WriteLine(i);
            }
        }
        public static void TimerHandler(object source, EventArgs e)
        {
            lock (_Lock)
            {
                Console.WriteLine("Event started");
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 100000000; j++) ;
                    Console.WriteLine("Event "+i);
                }
                Console.WriteLine("Event finished");
            }
        }
    }
}

You're using System.Timers.Timer, which processes ticks on a background thread, typically using the thread pool.

As such, yes, it will run simultaneously with the rest of your Main code.

As for your second question, possible to get Main to stop, I need to know what the expected behavior is.

If you want an entire tick in TimerHandler to run together, pausing Main while it does so, a simple lock would suffice:

namespace EventTest
{
    class Program
    {
        static object _Lock = new object();

        static void Main(string[] args)
        {
            Timer t = new Timer(2000);
            t.Enabled = true;
            t.Elapsed += new ElapsedEventHandler(TimerHandler);
            for (int i = 0; i < 10000; i++)
            {
                for (int j = 0; j < 100000000; j++) ;
                lock (_Lock)
                    Console.WriteLine(i);
            }
        }
        public static void TimerHandler(object source, EventArgs e)
        {
            lock (_Lock)
            {
                Console.WriteLine("Event started");
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < 100000000; j++) ;
                    Console.WriteLine("Event "+i);
                }
                Console.WriteLine("Event finished");
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文