“错误代码是0xc0000005。此错误可能是CLR中的错误”

时间:2014-05-08 22:38:20

标签: c# multithreading clr

首先让我解释一下我的计划以及正在发生的事情。 我的程序使用GSM调制解调器从移动卡读取消息,然后在Marquee中显示消息... 这个错误实际上是随机的,我在一个朋友的迪斯科舞厅测试了这个程序,有时它整晚都在工作而不会崩溃,而其他时候它会一遍又一遍地崩溃...... 所以,有一个类的代码可以完成Marquee(以及它崩溃的地方):

public class MyPanel : System.Windows.Forms.Panel
{

    private LinkedList<string> texto;
    private LinkedList<MensagemParaEcra> msgs;
    private LinkedList<Label> labels;
    private LinkedList<string> msgsRemover = new LinkedList<string>();
    private List<Label> labelsRmv = new List<Label>();
    private List<Label> labelARemover = new List<Label>();
    private Label firstLb;
    private Label lastLb;
    private Label leftLb;
    private Label lastLb3;

    private Color color;
    private Color backg;
    private Font textFont;
    private Label lastLb2;
    private Screen screen;

    private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer();

    public MyPanel(Color corLabel, Color back, Font text)
    {
        this.color = corLabel;
        this.backg = back;
        this.textFont = text;
        this.Width = 4000;

        texto = new LinkedList<string>();
        msgs = new LinkedList<MensagemParaEcra>();
        labels = new LinkedList<Label>();

        MouseMove += MyPanel_MouseMove;

        this.BackColor = backg;
        this.Size = new Size(4000, 69);
        this.Refresh();
        Screen[] screens = Screen.AllScreens;
        screen = Screen.FromControl(BarraSms.getInstance());
    }

    public void addMensagem(MensagemParaEcra msg) // add a message to be displayed in the monitor
    {
        Label lbl = new Label();
        lbl.Text = (msg.getTexto() + " -");
        lbl.ForeColor = color;
        lbl.Font = textFont;
        lbl.BackColor = backg;
        lbl.Visible = true;
        lbl.AutoSize = true;

        if (labels.Count > 0)
        {
            lbl.Location = new Point(this.lastLb3.Right, 0);
        }
        else
        {
            lbl.Location = new Point(screen.Bounds.Width + 10, 0);
            firstLb = lbl;
        }

        reorderAdd(lastLb3, lbl);
        labels.AddLast(lbl);

        this.Controls.Add(lbl);
        this.Refresh();
        this.lastLb3 = lbl;
    }

    private void reorderAdd(Label ultima, Label lbl) //reorder the list order when the user add a new message
    {
        var it = labels.OrderBy(x => x.Location.X);

        int count;
        for (int i = 0; i < labels.Count; i++)
        {
            if (it.ElementAt(i) == ultima)
            {
                count = i;
                lbl.Location = new Point(ultima.Right, 0);
                try
                {
                    if (it.ElementAt(i + 1) != null)
                        it.ElementAt(i + 1).Location = new Point(lbl.Right, 0);
                }catch{
                }
                for (int a = count+2; a < labels.Count; a++)
                {
                    if(it.ElementAt(a) != null)
                    it.ElementAt(a).Location = new Point(it.ElementAt(a-1).Right, 0);
                }
                return;
            }

        }
    }


    public void startT()
    {
        startThread();
    }

    public void removerMsg(string msg) //remove the message
    {
        string remover = msg + " -";  

        Label lb = getMsg(remover);

        labels.Remove(lb);
        this.Controls.Remove(lb); 
        this.Refresh();
        if(labels.Count >1)
        reorder();
    }

    private Label getMsg(string msg) //method to find a message in the list
    {
        var node = labels.First;
        while (node != null)
        {            
            var nextNode = node.Next;
            if (node.Value.Text.Equals(msg))
            {
                return node.Value;
            }
            node = nextNode;

        }
        return null;
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        this.ResumeLayout(false);

    }

    public void setVelocidade(int ve) { //used to set the interval of the timmer
        myTimer.Interval = ve;
    }
    private void startThread()
    {

        myTimer.Tick += new EventHandler(timerEvent);
        myTimer.Interval = 20;
        myTimer.Start();
    }

    private void MyPanel_MouseMove(object sender, MouseEventArgs e) //used to move the unborded window
    {
        BarraSms.getInstance().mouseMove(e);
    }

    [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute()] //I found this when I was searching for the error but it didn't help me
    private void timerEvent(object sender, EventArgs e)
    {
           var it4 = labels.OrderBy(x => x.Location.X);
           // var it = it4.GetEnumerator();

            //while (it.MoveNext())
            foreach(Label lb in it4)
            {
              //  Label lb = it.Current; //sometimes it crashes here

                if (firstLb == null)
                    firstLb = lb;

                if (lb.Location.X + lb.Width < 0) {
                    lb.Location = new Point(it4.Last().Right, 0);
                }
                else
                {
                    lb.Location = new Point(lb.Location.X - 4, lb.Location.Y);
                    leftLb = lb;
                }

                lastLb = lb;
            }
            this.Refresh(); //other times it crashes here
            if(labels.Count >2 )
            reorder();
    }

    private void reorder() { //used to reorder my list by the location on the screen
        var it2 = labels.OrderBy(x => x.Location.X);
        Label orderLb = it2.ElementAt(0);

        for (int i = 1; i < labels.Count; i++)
        {
            it2.ElementAt(i).Location = new Point(orderLb.Right, 0);
            orderLb = it2.ElementAt(i);
        }
    }

    public void applyDestaque(string msg) { //its just to highlight a message
        Label lb = getMsg(msg + " -");
        labels.Find(lb).Value.BackColor = Color.Yellow;
        labels.Find(lb).Value.ForeColor = Color.Black;
    }

    public void removeDestaque(string msg) { // remove the highlight
        Label lb = getMsg(msg + " -");
        labels.Find(lb).Value.BackColor = backg;
        labels.Find(lb).Value.ForeColor = color;
    }

    internal void addMensagemDestaque(MensagemParaEcra mensagemParaEcra)  // add a highlighted message
    {
        Label lbl = new Label();
        lbl.Text = (mensagemParaEcra.getTexto() + " -");
        lbl.ForeColor = Color.Black;
        lbl.Font = textFont;
        lbl.BackColor = Color.Yellow;
        lbl.Visible = true;
        lbl.AutoSize = true;

        if (labels.Count > 0)
        {
            lbl.Location = new Point(this.lastLb3.Right, 0);
        }
        else
        {
            lbl.Location = new Point(screen.Bounds.Width + 10, 0);
            firstLb = lbl;
        }

        reorderAdd(lastLb3, lbl);
        labels.AddLast(lbl);
        this.Controls.Add(lbl);
        this.Refresh();
        this.lastLb3 = lbl; 
    }
}

嗯,那就是......

我一遍又一遍地搜索,但我无法修复它或找到任何解决方案...... 编辑:到目前为止我所做的更改已在我的代码中更新,我评论了我在线程中的行,并添加了你告诉我做的foreach cicle

这是完整的错误:

  

运行时遇到了致命错误。错误的地址   位于0x5f44a62c处,位于线程0x2040上。错误代码是0xc0000005。   此错误可能是CLR中的错误,也可能是不安全或不可验证的错误   用户代码的一部分。此错误的常见来源包括用户   COM-interop或PInvoke的编组错误,可能会破坏   叠加。

编辑:为谁可以帮我解决这个错误添加了一笔赏金......

1 个答案:

答案 0 :(得分:-1)

documentation开始,您使用的属性需要[SecurityCritical]才能正常工作。来自文档:

The CLR delivers the corrupted process state exception to applicable exception clauses only in methods that have both the HandleProcessCorruptedStateExceptionsAttribute and SecurityCriticalAttribute attributes

您可以尝试添加[SecurityCritical]属性并捕获方法中的任何异常以记录它们,看看它是否为您提供了更多信息。