EventLoop for WinForm

Posted 力为

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EventLoop for WinForm相关的知识,希望对你有一定的参考价值。

   abstract class EventLoop
    
        protected static bool mExit = false;
        protected static int mCode = 0;
        protected static EventLoop mImpl;

        public static void Register(EventLoop impl)  mImpl = impl; 
        public static void Exit(int code)  mExit = true; mCode = code; 

        public abstract int Run();

        public static int Start()
        
            if (mImpl != null)
                return mImpl.Run();
            return 0;
        
    

    class EventLoopImpl : EventLoop
    
        System.Windows.Forms.Timer mTimer = new System.Windows.Forms.Timer();

        public override int Run()
        
            mTimer.Tick += new EventHandler(TimerEventProcessor);

            mTimer.Interval = 1000;
            mTimer.Start();
            mExit = false;
            while (!mExit)
            
                Application.DoEvents();
            

            return mCode;
        

        private void TimerEventProcessor(object sender, EventArgs e)
        
            mTimer.Stop();
            if (mExit == false)
            
                mTimer.Enabled = true;
            
        
    

 

使用方法:

    public partial class Form1 : Form
    
        public Form1()
        
            InitializeComponent();
            EventLoop.Register(new EventLoopImpl());
        
        private void button1_Click(object sender, EventArgs e)
        
            EventLoop.Exit(0);
        

        private void commandToolStripMenuItem_Click(object sender, EventArgs e)
        
            this.listView1.Items.Add("Step 1");
            EventLoop.Start();
            this.listView1.Items.Add("Step 2");
            EventLoop.Start();
            this.listView1.Items.Add("End");

        
    

 

以上是关于EventLoop for WinForm的主要内容,如果未能解决你的问题,请参考以下文章

EventLoop for WinForm

python多个eventloop报错

构建一个基于消息驱动的Eventloop线程模型

NanUI for Winform发布,让Winform界面设计拥有无限可能

急求:c# 在winform窗体中 用for循环输出的"*"三角形: * *** ***** *******

day040: 如何理解EventLoop——宏任务和微任务篇