软件让生活更美好 2016-10-19 07:46 采纳率: 0%
浏览 1186

winform运行了一段时间之后Program.cs报错

我想用winform尝试做一个雪花一样的特效,运行起来效果是对的,可是运行了一会之后,有时候几十分钟,有时候几个小时,突然就炸了,Program.cs报错,附代码,如图图片说明
图片说明

 public partial class Form1 : Form
    {
        int max = 20;
        double[] snowArray;
        Label[] labelArray;
        int newSnowNum = 0;
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            snowArray = new double[max];
            labelArray = new Label[max];
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false;
            CenterToScreen();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void move(int arrayNum)
        {
            if (labelArray[arrayNum] == null)
            {
                GetNewLabel(arrayNum);
            }
            int left = labelArray[arrayNum].Location.X;
            if (left <= 5 || left > 1000)
            {
                labelArray[arrayNum].Dispose();
                labelArray[arrayNum] = null;
                return;
            }
            int ran = new Random().Next(0, 2);
            if (ran == 1)
            {
                if (snowArray[arrayNum] < 6)
                {
                    snowArray[arrayNum] += 0.4;
                }
            }
            else
            {
                if (snowArray[arrayNum] > -6)
                {
                    snowArray[arrayNum] -= 0.4;
                }
            }
            labelArray[arrayNum].Location = new Point(labelArray[arrayNum].Location.X + (int)(snowArray[arrayNum] + 0.5 * (snowArray[arrayNum] > 0 ? 1 : -1)), (labelArray[arrayNum].Location.Y + (new Random().Next(0, 2) == 1 ? 2 : 1)));
            if (labelArray[arrayNum].Location.X > 1000 || labelArray[arrayNum].Location.X < 0 || labelArray[arrayNum].Location.Y > 500)
            {
                labelArray[arrayNum].Dispose();
                labelArray[arrayNum] = null;
                return;
            }
            SetTimeout(1000 / 24, arrayNum);
            //move(arrayNum);
        }

        private void GetNewLabel(int arrayNum)
        {
            Label lb = new Label();
            lb.Text = "hehe";
            lb.Name = Guid.NewGuid().ToString();
            lb.Size = new Size(29, 12);
            Random ran = new Random();
            int firstPositionLeft = ran.Next(0, 1000);
            lb.Location = new Point(firstPositionLeft, 0);
            labelArray[arrayNum] = lb;
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate { AddLabel(lb); }));
                return;
            }

            this.Controls.Add(lb);
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            move(0);
            SetInterval(1000);
        }

        private void AddLabel(Label lb)
        {
            this.Controls.Add(lb);
        }



        /// <summary> 
        /// 在指定时间过后执行指定的表达式 
        /// </summary> 
        /// <param name="interval">事件之间经过的时间(以毫秒为单位)</param> 
        /// <param name="action">要执行的表达式</param> 
        private void SetTimeout(double interval, int num)
        {
            System.Timers.Timer timer = new System.Timers.Timer(interval);
            timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
           {
               timer.Enabled = false;
               move(num);
           };
            timer.Enabled = true;
        }
        /// <summary> 
        /// 在指定时间周期重复执行指定的表达式 
        /// </summary> 
        /// <param name="interval">事件之间经过的时间(以毫秒为单位)</param> 
        /// <param name="action">要执行的表达式</param> 
        private void SetInterval(double interval)
        {
            System.Timers.Timer timer = new System.Timers.Timer(interval);
            timer.Elapsed += delegate(object sender, System.Timers.ElapsedEventArgs e)
            {
                if (newSnowNum < max - 1)
                {
                    move(++newSnowNum);
                }
                else
                {
                    newSnowNum = 0;
                    move(0);
                }
            };
            timer.Enabled = true;
        }

    }
  • 写回答

1条回答

  • zqbnqsdsmd 2016-10-20 03:04
    关注

    代码不正确,你需要完善下就可以了。

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作