循环中的c#EventHandler不会触发

时间:2017-07-07 22:49:30

标签: c# .net winforms events

我仍然是活动的菜鸟,我在使用eventhandler更新表单时遇到了麻烦。 我有一个表单,它是类StartPage中的一个计数器,这个计数器应该显示参数" bookQuantity"的值。这是在另一个类中调用Gauge计算的。我在while循环中订阅了eventHadler,但似乎从不使用函数Counter_Tick,在调试过程中通过CounterEvent传递并且不启动Counter_Tick函数。 这是代码(简化):

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace zeus
{
    public class Gauge : IGauge
    {
        private IObjectsToPrint objectsToPrint = new ObjectsToPrint();
        private IOutputMessages outputMessages = new OutputMessages();
        private decimal bookQuantity;
        private float counter = Parameters.GetcounterToPrint;
        public EventArgs e = null;

        public delegate void CounterHandler(Gauge g, EventArgs e);

        private StartPage target;
        private EventHandler CounterEvent;

        public Gauge()
        {
            List<Books> ListToPrint = Parameters.GetList;
            bookQuantity = Parameters.FirstQuantity;
        }

        private long theCounter;

        public long TheCounter
        {
            get
            {
                return theCounter;
            }
            set
            {
                theCounter = value;
                OnCounterChanged(this.theCounter);
            }
        }

        public async void GaugeCalculator()
        {
            var ItemList = Parameters.GetList;
            bookQuantity = Parameters.FirstQuantity;
            Parameters._counterToPrint = (long)bookQuantity;
            // CounterEvent += new EventHandler(Counter_Tick);

            if (Parameters.Getgaugespeedvalue == 0)
            {
                //do nothing as we are not running
            }
            else
            {
                foreach (var elem in ItemList)
                {
                    bookQuantity = Parameters.getQuantity(elem);

                    while (bookQuantity > 0)
                    {
                        await Task.Delay(TimeSpan.FromMilliseconds(20000 / (Parameters.Getgaugespeedvalue + 1)));
                        CounterEvent += new EventHandler(Counter_Tick);
                        bookQuantity--;
                    }
                    if (bookQuantity == 0)
                        outputMessages.PrintFinished(Parameters.getTitle(elem));
                    // MessageBox.Show("hello");
                }
            }
        }

        public delegate void CounterTickerHandler(long newCount);

        public event CounterTickerHandler TheCounterChanged;

        public void Counter_Tick(object sender, EventArgs e)
        {
            TheCounter = (long)bookQuantity;
        }

        protected void OnCounterChanged(long newCount)
        {
            if (TheCounterChanged != null)
            {
                TheCounterChanged(newCount);
            }
        }
    }
}

在另一个班级(表格)中我有:

namespace zeus
{
    public partial class StartPage : zeus.ParentForm
    {
        public StartPage()
        {
            InitializeComponent();
            this.gauge = new Gauge();
            this.gauge.TheCounterChanged += new Gauge.CounterTickerHandler(CounterHasChanged);
        }
    }
}

0 个答案:

没有答案