我的代码出了什么问题 [英] What's wrong with my code

查看:76
本文介绍了我的代码出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的用户控件继承自Label。

用户4位显示4个文本,就是它。

每次(

I have a very simple user control inherit from Label.
user 4 bit to show 4 Text,that's it.
everytime when(

CMS.S[_PlcAddress0] = true;

)和程序运行

plc0 = CMS.S[_PlcAddress0];

,Winform UI卡住,但cpu和内存似乎没什么变化。

",Winform UI stuck,but cpu and memory looks like nothing change.

<pre lang="c#">
<pre>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MyControls
{
    public class vText4Pos : Label
    {
        public vText4Pos()
        {           
            InitializeComponent();
            Communication.EvenNewData += Fresh;
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
        private int _PlcAddress0 = 0;
        [Category("UserSetting")]
        public int PlcAddress0
        {
            get { return _PlcAddress0; }
            set
            {
                if (value != _PlcAddress0 & value >= 0 & value <= CMS.S.Length - 1)
                {
                    _PlcAddress0 = value;
                }
            }
        }
        private int _PlcAddress1 = 0;
        [Category("UserSetting")]
        public int PlcAddress1
        {
            get { return _PlcAddress1; }
            set
            {
                if (value != _PlcAddress1 & value >= 0 & value <= CMS.S.Length - 1)
                {
                    _PlcAddress1 = value;
                }
            }
        }
        private int _PlcAddress2 = 0;
        [Category("UserSetting")]
        public int PlcAddress2
        {
            get { return _PlcAddress2; }
            set
            {
                if (value != _PlcAddress2 & value >= 0 & value <= CMS.S.Length - 1)
                {
                    _PlcAddress2 = value;
                }
            }
        }
        private int _PlcAddress3 = 0;
        [Category("UserSetting")]
        public int PlcAddress3
        {
            get { return _PlcAddress3; }
            set
            {
                if (value != _PlcAddress3 & value >= 0 & value <= CMS.S.Length - 1)
                {
                    _PlcAddress3 = value;
                }
            }
        }
        private string text0="";
        [Category("UserSetting"), Localizable(true)]
        public string Text0
        {
            get { return text0; }
            set
            {
                if (text0 != value)
                {
                    text0 = value;
                }
            }
        }
        private string text1="";
        [Category("UserSetting"), Localizable(true)]
        public string Text1
        {
            get { return text1; }
            set
            {
                if (text1 != value)
                {
                    text1 = value;
                }
            }
        }
        private string text2="";
        [Category("UserSetting"), Localizable(true)]
        public string Text2
        {
            get { return text2; }
            set
            {
                if (text2 != value)
                {
                    text2 = value;
                }
            }
        }
        private string text3="";
        [Category("UserSetting"), Localizable(true)]
        public string Text3
        {
            get { return text3; }
            set
            {
                if (text3 != value)
                {
                    text3 = value;
                }
            }
        }
        private string textDefault="Default text";
        [Category("UserSetting"), Localizable(true)]
        public string TextDefault
        {
            get { return textDefault; }
            set
            {
                if (textDefault != value)
                {
                    textDefault = value;
                    Invalidate();
                }
            }
        }
        private Color colorDefault=Color.LimeGreen;
        [Category("UserSetting")]
        public Color ColorDefault
        {
            get { return colorDefault; }
            set
            {
                if (value != colorDefault)
                {
                    colorDefault = value;
                    Invalidate();
                }
            }
        }
        Color color0 = Color.Yellow;
        [Category("UserSetting")]
        public Color Color0
        {
            get {return color0; }
            set
            {
                if (value!= color0)
                {
                    color0 = value;
                }
            }
        }
        Color color1 = Color.LimeGreen;
        [Category("UserSetting")]
        public Color Color1
        {
            get { return color1; }
            set
            {
                if (value != color1)
                {
                    color1 = value;
                }
            }
        }
        Color color2 = Color.Blue;
        [Category("UserSetting")]
        public Color Color2
        {
            get { return color2; }
            set
            {
                if (value != color2)
                {
                    color2 = value;
                }
            }
        }
        Color color3 = Color.Red;
        [Category("UserSetting")]
        public Color Color3
        {
            get { return color3; }
            set
            {
                if (value != color3)
                {
                    color3 = value;
                }
            }
        }
        private void update()
        {
            Text = textDefault;
            BackColor = colorDefault;
            if(_PlcAddress0 != 0 & plc0)
            {
                BackColor = color0;
                Text = text0;
            }
            if (_PlcAddress1 != 0 & plc1)
            {
                BackColor = color1;
                Text = text1;
            }
            if (_PlcAddress2 != 0 & plc2)
            {
                BackColor = color2;
                Text = text2;
            }
            if (_PlcAddress3 != 0 & plc3)
            {
                BackColor = color3;
                Text = text3;
            }
        }
        private void vText_Paint(object sender, PaintEventArgs e)
        {
            AutoSize = false;
            update();
        }

        private bool plc0 = false;
        private bool plc1 = false;
        private bool plc2 = false;
        private bool plc3 = false;

        private void Fresh()
        {
            if(plc0 != CMS.S[_PlcAddress0])
            {
                try
                {
                    plc0 = CMS.S[_PlcAddress0];
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                update();
            }
        }
        #region 组件设计器生成的代码
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
        /// <summary> 
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // vText4Pos
            // 
            this.Margin = new System.Windows.Forms.Padding(2);
            this.Size = new System.Drawing.Size(140, 16);
            this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.vText_Paint);
            this.ResumeLayout(false);

        }
        #endregion
    }
}







我尝试过:






What I have tried:

If close this form and change <pre>CMS.S[_PlcAddress0]=false;

并再次打开表单,一切恢复正常。然后如果设置

and open form again,everything back to normal. then if set

CMS.S[_PlcAddress0]=true

,winform再次卡住。

,winform stuck again.

推荐答案

不运行代码 - 而且我们没有足够的能力做到这一点,或者如果我们做的话,任何想法如何使用它 - 你在某个地方被困在一个循环中,你需要从找到位置开始。



首先在调试器中运行代码。当它被锁定时,使用Visual Studio菜单项Debug ... Break,这将导致您的应用停在原地。如果可以的话,VS会尝试显示它正在执行哪一行,如果不能,它会有一个堆栈跟踪。



看看你在哪里,并在代码的最后执行行上放置一个断点。停止调试,重新启动,然后再次执行。这一次,它应该在到达断点时停止,让你逐行浏览以找出发生的情况,同时查看变量内容并查看它们是如何受到影响的。



很抱歉,但我们不能为你做到这一点 - 学习新技能的时间:调试!
Without running your code - and we don't have enough to do that, or any idea how to use it if we did - you are stuck in a loop somewhere, and you need to start by finding out where.

Start by running your code in the debugger. When it gets "locked up", use the Visual Studio menu item "Debug ... Break" which will cause your app to stop where it is. VS will try and show you which line it is executing if it can, and will have a stack trace available if it can't.

Look at where you are, and put a breakpoint on the last executed line of your code. Stop debugging, restart, and do it again. This time, it should stop when it reaches the breakpoint, and let you trace through line by line to find out what is happening, while you look at the variables content and see how they are affected.

Sorry, but we can't do that for you - time to learn a new skill: debugging!


这篇关于我的代码出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆